开发者

Why is my RegExp ignoring start and end of strings?

开发者 https://www.devze.com 2023-03-24 06:28 出处:网络
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开发者_JAVA百科 sentence, why is that?

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开发者_JAVA百科 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


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.


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.


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消