Reputation: 36391
I made this helper function to find single words, that are not part of bigger expressions
it works fine on any word that is NOT first or last in a sentence, why is that?
is there a way to add ""
to regexp?
String.prototype.findWord = function(word) {
var startsWith = /[\[\]\.,-\/#!$%\^&\*;:{}=\-_~()\s]/ ;
var endsWith = /[^A-Za-z0-9]/ ;
var wordIndex = this.indexOf(word);
if (startsWith.test(this.charAt(wordIndex - 1)) &&
endsWith.test(this.charAt(wordIndex + word.length))) {
return wordIndex;
}
else {return -1;}
}
Also, any improvement suggestions for the function itself are welcome!
UPDATE: example: I want to find the word able
in a string, I waht it to work in cases like [able] able, #able1
etc.. but not in cases that it is part of another word like disable, enable
etc
Upvotes: 0
Views: 145
Reputation: 5857
If you want your endsWith
regexp also matches the empty string, you just need to append |^$
to it:
var endsWith = /[^A-Za-z0-9]|^$/ ;
Anyway, you can easily check if it is the beginning of the text with if (wordIndex == 0)
, and if it is the end with if (wordIndex + word.length == this.length)
.
It is also possible to eliminate this issue by operating on a copy of the input string, surrounded with non-alphanumerical characters. For example:
var s = "#" + this + "#";
var wordIndex = this.indexOf(word) - 1;
But I'm afraid there is another problems with your function:
it would never match "able" in a string like "disable able enable" since the call to indexOf
would return 3, then startsWith.test(wordIndex)
would return false
and the function would exit with -1 without searching further.
So you could try:
String.prototype.findWord = function (word) {
var startsWith = "[\\[\\]\\.,-\\/#!$%\\^&\*;:{}=\\-_~()\\s]";
var endsWith = "[^A-Za-z0-9]";
var wordIndex = ("#"+this+"#").search(new RegExp(startsWith + word + endsWith)) - 1;
if (wordIndex == -1) { return -1; }
return wordIndex;
}
Upvotes: 1
Reputation: 45525
A different version:
String.prototype.findWord = function(word) {
return this.search(new RegExp("\\b"+word+"\\b"));
}
Your if
will only evaluate to true if endsWith
matches after the word. But the last word of a sentence ends with a full stop, which won't match your alphanumeric expression.
Upvotes: 2
Reputation: 11232
Did you try word boundary -- \b
?
There is also \w
which match one word character ([a-zA-Z_]
) -- this could help you too (depends on your word definition).
See RegExp docs for more details.
Upvotes: 1