0
[SOLVED] Small problem with regex match() & replace()
I have a small piece of code like this: a:no(semicolon) { h:hello } If there is no presence of ; in the last statement before '}', I need to add it using Javascript and regex. And I tried in some way, but it is placing ; at wrong place. Result: a:no(semicolon) { h:hello }; Expected: a:no(semicolon) { h:hello; } My try: https://code.sololearn.com/W5n2Ccs2iU0M/?ref=app
2 Réponses
+ 2
Since you need a semicolon after the word, not after the whole match, the easiest thing to do would be to capture separately the two parts and in replacement string put the semicolon between the captures. Like this:
regexp: /(\w+)(\s*})/g
replacement: "$1;$2"
+ 1
Volodymyr Chelnokov Thank you.