My problem is i want to do something like this:
Javascript vaja => <b>Ja</b>vascript va<b>ja</b>
ie i have a query string(ja) and i want to replace all the occurances(case insensitive) of that query string in a bigger string (Javascript vaja).
The closest solution i have right now is:
"Javascript vaja".replace(/ja/gi, '<b>ja</b>');
which gives me:
"<b>ja</b>vascript va<b>ja</b>"
but what i need is:
Javascript vaja => <b>Ja</b>vascript va<b>ja</b>
one solution that i have in mind is to keep the indexes of upcase letters before replacement and then re replace them. But that is way too hacky. I am pretty sure i am not the first one trying this and pretty sure there is some elegant and simpler solution hidden somewhere.
Simply use a capturing group:
"Javascript vaja".replace(/(ja)/gi, '<b>$1</b>');
Edit: Read more about capturing groups here.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.