I have the following task - there is a multiline text in var text where I need to do the following replacement - if line starts from one or two numbers and point e.g. 1. or 20. need to replace it with li element. I've tried to do it with regexp but if there are numbers after the line start it will be replaced also and it's not a point.
Could anyone help me with right regexp?
let text =`
1.
Some text here
Any text here
Some text here
2.
Some text here
Any text here 24
Some text here
30.
Some text here
Any text here
Some text here`;
let regex = /[0-9]./g;
let result = text.replace(regex, '<li>');
document.write(result);
CodePudding user response:
If you need to replace number with LI and number, use parentheses to create capture group and then reference it with dollar sign:
s.replace( /(\d \.)/g , "<li>$1" )
CodePudding user response:
Your regex must be let regex = /[0-9] \./g;
let text =`
1.
Some text here
Any text here
Some text here
2.
Some text here
Any text here 2400
Some text here00
300.
Some text here
Any text here
Some text here`;
let regex = /[0-9] \./g;
let result = text.replace(regex, '<li>');
document.write(result);