[英]RegExp - how to match only those words which are in boundaries? \b not working
Let's say I have a string "btn btn-block btn-red btn-". 假设我有一个字符串“ btn btn-block btn-red btn-”。 How do I find only "btn-" ?
如何仅找到“ btn-”? In JavaScript \\b works fine for "btn-block" but it breaks when it ends with a hyphen.
在JavaScript中,\\ b可以很好地用于“ btn-block”,但在以连字符结尾时会中断。 When I use /\\bbtn-\\b/ it selects "btn-block" and "btn-red" but not the actual "btn-" which I want.
当我使用/ \\ bbtn- \\ b /时,它将选择“ btn-block”和“ btn-red”,而不是我想要的实际“ btn-”。 Is there a workaround for this?
有没有解决方法?
与此相关的Group 1
应该工作\\b(btn-)(\\s|$)
使用此/btn-/g
或/btn-/gi
忽略/btn-/gi
,请尝试此http://www.regviz.org/
It depends on what you mean by "find". 这取决于您“查找”的意思。 If you mean "If it exists in the string, return true" then this will work:
如果您的意思是“如果字符串中存在它,则返回true”,那么它将起作用:
var myString = "btn btn-block btn-red btn-";
var myStringTwo = "btn btnblock";
// This will print "Found btn- in myString"
if(myString.search(/btn-/) > -1)
console.log("Found btn- in myString");
else
console.log("Couldn't find btn- in myString");
// This will print "Couldn't find btn- in myStringTwo"
if(myStringTwo.search(/btn-/) > -1)
console.log("Found btn- in myStringTwo ");
else
console.log("Couldn't find btn- in myStringTwo");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.