0

I need to match keywords and replace those keywords with <a /> html or <span></span> tags. Script which i am working on match only single words with no space if i use following regex, \w

text = text.replace(
            /([\u0600-\u06ff\w]+)([^\u0600-\u06ff]+)()?/g,
        replacer);

and if i use following regex with \s option then it match all words wrapped withing ASCII such as , ", ' etc.. in fiddle example you will notice it will match all keywords for this from the first paragraphs and which a separated by ,

   text2 = text2.replace(
        /([\u0600-\u06ff\s]+)([^\u0600-\u06ff]+)()?/g,
    replacer);

How can i modify this script so that it works with combinations and match only exact words. I have been working with for few days i tried few option also but non seems to be working, i tried highlight plugin that also had its own issue with arabic. question raised for highlight plugin also here https://stackoverflow.com/questions/29533793/highlighting-of-text-breaks-when-for-either-english-or-arabic

Regex Matching : http://jsfiddle.net/u3k01bfw/13/

Community
  • 1
  • 1
Learning
  • 19,469
  • 39
  • 180
  • 373
  • See http://stackoverflow.com/questions/2881445/utf-8-word-boundary-regex-in-javascript – bdrx Apr 13 '15 at 04:41

1 Answers1

0

It is possible that changing ()? to (?=\s|$) (that will limit the matches up to a space or a string end) can fix your issue:

text = text.replace(
        /([\u0600-\u06ff\w]+)([^\u0600-\u06ff]+)(?=\s|$)/g,
    replacer);

text2 = text2.replace(
        /([\u0600-\u06ff\s]+)([^\u0600-\u06ff]+)(?=\s|$)/g,
    replacer);

Please check http://jsfiddle.net/u3k01bfw/17/.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563