问题
Im trying this example
first example
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");
}
Example output -> found it
second example
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");
}
output -> found it
but i want to find only whole word. punctuation issue for me how can i using regex for second example output: did not find it
回答1:
Coldfusion does not support lookbehind, so, you cannot use a real "zero-width boundary" check. Instead, you can use groupings (and fortunately a lookahead):
regEx = "(^|\W)"& keyword &"(?=\W|$)";
Here, (^|\W)
matches either the start of a string, and (?=\W|$)
makes sure there is either a non-word character (\W
) or the end of string ($
).
See the regex demo
However, make sure you escape your keyword before passing to the regex. See ColdFusion 10 now provides reEscape() to prepare string literals for native RE-methods.
Another way is to match spaces or start/end of string:
<cfset regEx = "(^|\s)" & TABLE_NAME & "($|\s)">
来源:https://stackoverflow.com/questions/33960491/regex-match-whole-word-string-in-coldfusion