- Home /
Question by
Lemo76 · Nov 08, 2014 at 03:45 AM ·
javascriptstringkeyword
Check for a Single word in a String (JS)
So I've got this string:
This was a sentence
Now I want it to to check if the word 'is' is in the sentence. Using most methods e.g.
(Sentence.Contains("is"))
("is" in Sentence)
It will output as 'true', because it is looking at the word "This" and getting the "is" from that.
How would I get it so it only matches to whole words seperated by spacing?
Thanks!
Comment
Answer by Eric5h5 · Nov 08, 2014 at 03:58 AM
The same as in C#; Regex.$$anonymous$$atch and so on are .NET functions that are usable in any language.
Correct me if I'm wrong, but I've tried
print (Regex.$$anonymous$$atch("This was a sentence", @"\b" + "is" + @"\b", RegexOptions.Singleline | RegexOptions.IgnoreCase));
and
print (Regex.Is$$anonymous$$atch("This was a sentence", "is"));
neither seem to work and I am very unsure on the syntaxing/formatting as this is a grey area to me.
Debug.Log (Regex.$$anonymous$$atch("This was a sentence", @"\bis\b"));
Get rid of the "@", since JS doesn't have that.
Debug.Log (Regex.$$anonymous$$atch("This was a sentence", "\\bis\\b").Success);
Your answer
![](https://koobas.hobune.stream/wayback/20220613171011im_/https://answers.unity.com/themes/thub/images/avi.jpg)