正则表达式在coldfusion中匹配整个单词串

Ege*_*tes 4 regex coldfusion

我试着这个例子

第一个例子

keyword = "star"; 
myString = "The dog sniffed at the star fish and growled";
regEx = "\b"& keyword &"\b"; 
if (reFindNoCase(regEx, myString)) { 
     writeOutput("found it"); 
} else { 
     writeOutput("did not find it"); 
} 
Run Code Online (Sandbox Code Playgroud)

示例输出 - >找到它

第二个例子

keyword = "star"; 
myString = "The dog sniffed at the .star fish and growled";
regEx = "\b"& keyword &"\b"; 
if (reFindNoCase(regEx, myString)) { 
     writeOutput("found it"); 
} else { 
     writeOutput("did not find it"); 
}
Run Code Online (Sandbox Code Playgroud)

输出 - >找到它

但我想找到整个单词.标点问题对我来说如何使用正则表达式进行第二个示例输出:没找到它

Wik*_*żew 5

Coldfusion不支持lookbehind,因此,您不能使用真正的"零宽度边界"检查.相反,您可以使用分组(幸运的是前瞻):

regEx = "(^|\W)"& keyword &"(?=\W|$)";
Run Code Online (Sandbox Code Playgroud)

在这里,(^|\W)匹配字符串的开头,并(?=\W|$)确保存在非单词字符(\W)或字符串结尾($).

请参阅正则表达式演示

但是,请确保在传递给正则表达式之前转义关键字.请参阅ColdFusion 10现在提供reEscape()来为本机RE方法准备字符串文字.

另一种方法是匹配空格或字符串的开头/结尾:

<cfset regEx = "(^|\s)" & TABLE_NAME & "($|\s)">
Run Code Online (Sandbox Code Playgroud)