Javascript find index of word in string (not part of word)
Asked Answered
S

3

17

I am currently using str.indexOf("word") to find a word in a string. But the problem is that it is also returning parts of other words.

Example: "I went to the foobar and ordered foo." I want the first index of the single word "foo", not not the foo within foobar.

I can not search for "foo " because sometimes it might be followed by a full-stop or comma (any non-alphanumeric character).

Selfsacrifice answered 8/10, 2012 at 0:4 Comment(2)
You have six questions which don't have accepted answers. Go back and accept an answer and people will be willing to post to this one.Gild
Ok thanks I did not know I had a persistant account as "user759885", I have gone through and done so.Selfsacrifice
S
31

You'll have to use regex for this:

> 'I went to the foobar and ordered foo.'.indexOf('foo')
14
> 'I went to the foobar and ordered foo.'.search(/\bfoo\b/)
33

/\bfoo\b/ matches foo that is surrounded by word boundaries.

To match an arbitrary word, construct a RegExp object:

> var word = 'foo';
> var regex = new RegExp('\\b' + word + '\\b');
> 'I went to the foobar and ordered foo.'.search(regex);
33
Salsbury answered 8/10, 2012 at 0:9 Comment(9)
Had a go at making a general case but my implementation doesn't seem to be working. reg = '/\b' + word+ '\b/'; str.indexOf(reg);Selfsacrifice
If you want to compose the regex you need two steps: double scape the "\" ("\\b") and call the RegExp constructor: reg = new RegExp("\\b" + word + "\\b");Toga
Ah, I see indexOf does not take regex so you have used search(). My final solution was to use reg = '\\b(' + word + ')\\b'; str.indexOf(reg);Selfsacrifice
@user759885: See my edit. You'll have to construct a RegExp object.Salsbury
Its not proper solution. for example, var word = '.'; var regex = new RegExp('\\b' + word + '\\b'); alert('I went to the foobar and ordered foo.'.search(regex)); it will give 0.Plowman
@MMTac: JavaScript doesn't have an equivalent of Python's re.escape(), so the dot isn't being treated as a literal dot.Salsbury
@Blender: Thanks for such a guidence.Plowman
@Salsbury FYI \\b boundaries in javascript do not work for non-English but otherwise perfectly alphanumeric characters like à, é, ö. new RegExp("\\bestá\\b").test('¿dónde está la alcaldesa?') returns False, but curiously new RegExp("\\bdónde\\b").test('¿dónde está la alcaldesa?') returns True.Decane
@Growler did you know how to solve this issue you are talking about? I have the same issue on a translation script that's not working with the string "se acordó" because it has a "ó" at the end.Malayalam
O
7

For a general case, use the RegExp constrcutor to create the regular expression bounded by word boundaries:

function matchWord(s, word) {
  var re = new RegExp( '\\b' + word + '\\b');
  return s.match(re);
}

Note that hyphens are considered word boundaries, so sun-dried is two words.

Oatcake answered 8/10, 2012 at 0:23 Comment(0)
D
0

I have tried both with ".search" and ".match", as suggested in the previous answers, but only this solution worked for me.

var str = 'Lorem Ipsum Docet';
var kw  = 'IPSUM';
var res = new RegExp('\\b('+kw+')\\b','i').test(str);

console.log(res); // true (...or false)

With the 'i' for case insensitive search.

ComFreek wrote a detailed answer here

Decurrent answered 19/2, 2020 at 21:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.