I have created a system to convert the numbers you enter into lists.
This is my code:
HTML
<textarea id="input" cols="80" rows="4"></textarea>
<br>
<br>
<button onclick="compile()"> Parse </button>
<br>
<br>
<p id="output"></p>
JS
const parser = (text) => {
const parsed = text
.replace(/^1. (.*$)/gim, '<ol><li>$1</li></ol>');
return parsed.trim()
}
const compile = () => {
document.getElementById("output").innerHTML = parser(document.getElementById("input").value);
}
Here, when I execute the code as such: 1. foo 1
, I get the result as expected:
- foo 1
But when I execute:
- foo 1
- foo 2
It doesn’t work. Is there any method that could enable me to type numbers into bullets that drag onto ∞?
In JavaScript, you can use the .replace()
method to replace multiple numbers in a string. Here’s an example:
html code
<!DOCTYPE html>
<html>
<body>
<p id="demo">This is a test string with some numbers: 123 456 789.</p>
<script>
var str = document.getElementById("demo").innerHTML;
var replacedStr = str.replace(/123|456|789/g, "XXX");
document.getElementById("demo").innerHTML = replacedStr;
</script>
</body>
</html>
In the example above, we use the .replace()
method to replace the numbers “123”, “456”, and “789” in the string with the string “XXX”.
To replace multiple numbers, we can use a regular expression with the g
flag to perform a global search and replace all occurrences of the numbers in the string. In this case, we use the regular expression /123|456|789/g
to match any occurrence of “123”, “456”, or “789” in the string.
@aditya2010.kadiyala @aditya2010.kadiyala
Hi @sahanasahu848
Thanks for the solution. But can you suggest any methods using JavaScript RegEx
?
Use the replace()
method to replace multiple characters in a string, e.g. str.replace(/[._-]/g, ' ')
.
The first parameter the method takes is a regular expression that can match multiple characters.
const str = 'one.two_three-four';
const result = str.replace(/[._-]/g, ' ');
console.log(result);