最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript Regex for a partial string match only - Stack Overflow

programmeradmin2浏览0评论

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!

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!

Share Improve this question asked May 28, 2014 at 9:41 maurya8888maurya8888 2531 gold badge4 silver badges11 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 9

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

/\Batta|atta\B/

sp00m almost got it right

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

Fiddle.

If whitespace is not allowed you could write

^(atta[\S]+|[\S]+atta|[\S]+atta[\S]+)$
发布评论

评论列表(0)

  1. 暂无评论