Surround Hebrew and English text in div

后端 未结 3 1117
慢半拍i
慢半拍i 2021-01-12 04:45

I am trying to add a span tag around Hebrew and English sentence in a paragraph. E.g. \"so היי all whats up אתכם?\" will become :

[span]so[/span][span]היי         


        
3条回答
  •  一向
    一向 (楼主)
    2021-01-12 05:13

    Judging by this post you can try something like this: ((?:\s*\w+)+|(?:\s*[\u0590-\u05FF]+)+?(?=\s?[A-Za-z0-9!?.])) https://regex101.com/r/kA3yV5/4

    You may need to edit it for your particular cases (for example, if some non-word characters start to appear), but it does the trick. It tries to match words and form sentences from English character list, if it doesn't work, it tries to make words/sentences out of Hebrew character list, until an english character is spotted again.

    It's not perfect yet, as you may want to add other punctuation characters and there's some spaces you don't want in the 1st position (because javascript doesn't support lookbehinds, I didn't figure out a good way to remove them on the spot, but they can be at position 1 and removed from string)

提交回复
热议问题