![](/img/trans.png)
[英]Search a string for any occurence of a word in a list of strings and higlight matched word if first character of word matches.Javascript regex
[英]First occurence of a word in a text with JavaScript Regex
我有一個文本,其中出現了6個“海洋”事件。
我想在此文本中僅找到該詞的首次出現,並以“ Plane”代替。
我嘗試使用此RegEx:
var myRegEx = new RegExp("^(.*)Marine(.*)$","gmi");
但這給了我第四,第五,第六次出現...
您也可以不使用regex
"Marine Marine Marine Marine Marine Marine".replace('Marine','Plane')
//"Plane Marine Marine Marine Marine Marine"
我想在此文本中僅找到該詞的首次出現,並以“ Plane”代替。
var myRegEx = new RegExp("Marine","i");
要么
\\\\b
單詞邊界有助於精確匹配。
var myRegEx = new RegExp("\\bMarine\\b","i");
要么
str.replace(/\bmarine\b/i)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.