How can I find a word with regular expression in Javascript?
For example:
http://127.0.0.1/w/?id=2&tab=wow
I want to know if this link contains the word 'tab'.
How can I find a word with regular expression in Javascript?
For example:
http://127.0.0.1/w/?id=2&tab=wow
I want to know if this link contains the word 'tab'.
var string = 'http://127.0.0.1/w/?id=2&tab=wow'
var containsTab = string.indexOf('tab') > -1
Or if you really want to use a regex:
var containsTab = string.match(/tab/);
indexOf() returns -1 for not-found./\btab\b/. This is so that words like stable do not trigger a match.word and not a substring. (Maybe he even wanted to know whether there is a query string parameter named tab but didn't ask properly; I'm not going into that.)Use indexOf.
var str = 'http://127.0.0.1/w/?id=2&tab=wow';
if(str.indexOf('tab') > -1) {
// Contains string
} else {
// Doesn't
}
stable contain the word tab? In your case, yes. And this is obviously wrong answer.stable does contain the word tab.