maurya8888
maurya8888

Reputation: 253

Javascript Regex for a partial string match only

I need to write a JavaScript RegEx that matches only the partial strings in a word.

For example if I search using the string 'atta'

it should return

true for khatta
true for attari
true for navrattan
false for atta

I am not able to figure how to get this done using one RegEx. Thanks!

Upvotes: 4

Views: 19654

Answers (2)

Lee Kowalkowski
Lee Kowalkowski

Reputation: 11751

You want to use a non-word boundary \B here.

/\Batta|atta\B/

Upvotes: 9

sp00m almost got it right

^(atta.+|.+atta|.+atta.+)$

Fiddle.

If whitespace is not allowed you could write

^(atta[\S]+|[\S]+atta|[\S]+atta[\S]+)$

Upvotes: 2

Related Questions