0

I want to catch word with non-space.

var paragraphy="Apple banana kişiler ki örnek foo.";
var word="kişiler"; 
var regex = new RegExp("(?:[\\s>]|^)("+word+")(?=[.,;?!]?(?:[<\\s])|$)", "gi");
console.log(paragraphy.match);

This prints: [" kişiler"] but I want this result: ["kişiler"] I must use Positive Lookbehind but it is not supported in JavaScript. Is there an alternative solution?

iggymoran
  • 4,059
  • 2
  • 21
  • 26
ozen
  • 47
  • 7

2 Answers2

1

Since you can't use word boundaries with unicode characters, let's match the spaces but group the word to retrieve it :

(?:^|\s)(<your word>)(?:\s|$)

After a match, group 1 should be the expected word.

For instance :

var regex = /(?:^|\s)(kişiler)(?:\s|$)/
var paragraphy="Apple banana kişiler ki örnek foo."
paragraphy.match(regex)[1]
> "kişiler"
Aaron
  • 24,009
  • 2
  • 33
  • 57
  • thanks but result: [" kişiler "] why catch with space? – ozen Apr 28 '16 at 09:20
  • because as you said there's no lookbehind, so you must catch everything you want to check and then only retrieve the interesting part thanks to grouping. `paragraphy.match(regex)[1]` in my example is retrieving group 1 of the match, which corresponds to your specified word. – Aaron Apr 28 '16 at 09:22
  • My English is not good enough, that's why you find it difficult to express myself and understand. even so your solution is work Thank yo very much. – ozen Apr 28 '16 at 09:30
1

JS Demo

var paragraphy="Apple ba kişiler nana ki örnek foo.";
var word="kişiler"; 
var regex = new RegExp("(?:^|\\s)("+word+")(?=\\s|$)", "gi");
var m = regex.exec(paragraphy);
document.writeln(m[1]);
rock321987
  • 10,942
  • 1
  • 30
  • 43