I have a text where there are 6 occurrences of "Marine".
I want to find in this text the first occurrence of the word only and replace it by "Plane" for example.
I tried with this RegEx:
var myRegEx = new RegExp("^(.*)Marine(.*)$","gmi");
But it gives me the 4th, 5th, 6th occurrences...
You can do it without regex
also
"Marine Marine Marine Marine Marine Marine".replace('Marine','Plane')
//"Plane Marine Marine Marine Marine Marine"
I want to find in this text the first occurrence of the word only and replace it by "Plane" for example.
var myRegEx = new RegExp("Marine","i");
or
\\\\b
word boundary helps to do a exact match.
var myRegEx = new RegExp("\\bMarine\\b","i");
or
str.replace(/\bmarine\b/i)
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.