I have a string in the following format.
我有一个以下格式的字符串。
"ad60 ad60-12 ad60 12-ad60"
“ad60 ad60-12 ad60 12-ad60”
now I want to find the matches only where "ad60" is written.
现在我想找到只写“ad60”的匹配项。
I started off with
我开始了
/\bad60\b/i
/ \ bad60 \ B / I
but regex finds that '-' is not part of the character string. which returns 4 matches. I tried many things but they all either return 4 matches or return nothing.
但正则表达式发现' - '不是字符串的一部分。返回4场比赛。我尝试了很多东西,但他们要么返回4场比赛,要么什么也不返回。
Any kind of help would be appreciated.
任何形式的帮助将不胜感激。
3 个解决方案
#1
2
You can use:
您可以使用:
var s = "ad60 ad60-12 ad60 12-ad60";
var r = s.replace(/(^|\s)ad60(?=\s|$)/g, "$1@@");
//=> @@ ad60-12 @@ 12-ad60
#2
0
this should find any word starting from letter
这应该找到从字母开始的任何单词
/[a-z][a-z\d]+/i
ADDED
添加
if you wish to find any words which are included ad60 string try this
如果你想找到包含ad60字符串的任何单词,试试这个
/[a-z\d\-\_]*ad60[a-z\d\-\_]*/i
but words separated by space will not get in result
但是用空格分隔的单词不会得到结果
#3
0
"ad60 ad60-12 ad60 12-ad60".match(/(^ad60\s+)|(\s+ad60$)|(\s+ad60\s+)|(^ad60$)/ig);
will result in ["ad60 ", " ad60 "] then you can trim the white spaces in matched elements.
将导致[“ad60”,“ad60”]然后您可以修剪匹配元素中的空白区域。
#1
2
You can use:
您可以使用:
var s = "ad60 ad60-12 ad60 12-ad60";
var r = s.replace(/(^|\s)ad60(?=\s|$)/g, "$1@@");
//=> @@ ad60-12 @@ 12-ad60
#2
0
this should find any word starting from letter
这应该找到从字母开始的任何单词
/[a-z][a-z\d]+/i
ADDED
添加
if you wish to find any words which are included ad60 string try this
如果你想找到包含ad60字符串的任何单词,试试这个
/[a-z\d\-\_]*ad60[a-z\d\-\_]*/i
but words separated by space will not get in result
但是用空格分隔的单词不会得到结果
#3
0
"ad60 ad60-12 ad60 12-ad60".match(/(^ad60\s+)|(\s+ad60$)|(\s+ad60\s+)|(^ad60$)/ig);
will result in ["ad60 ", " ad60 "] then you can trim the white spaces in matched elements.
将导致[“ad60”,“ad60”]然后您可以修剪匹配元素中的空白区域。