[SOLVED] Small problem with regex match() & replace() | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

17th Nov 2020, 9:13 AM
Bibek Oli
Bibek Oli - avatar
2 Answers
+ 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"
17th Nov 2020, 9:24 AM
Volodymyr Chelnokov
Volodymyr Chelnokov - avatar
+ 1
17th Nov 2020, 9:35 AM
Bibek Oli
Bibek Oli - avatar