Identify and isolate hebrew word with regexp

不打扰是莪最后的温柔 提交于 2019-12-10 17:41:51

问题


I need to parse a hebrew sentence to identify and isolate (so I can wrap them up with 'span' tags) every word in it. I'm first trying without punctuation by identifying the non spaces chars from the spaces chars but still doesn't work:

var regex = /(\s)*(\S)+(\s)*/g;

any idea? thanks

edit: I have already a regular expression that does the job in english, I'm putting it in case it can help to understand what I want to achieve:

var regExp = /\b([^\s']+)\b/g,

edit2: Adding code example

var regex = /(\s)*(\S)+(\s)*/g;
var sentence = "שלום מה קורה מהיום";
sentence.replace(regex, function(match, p1, p2, p3){console.log('"' + match + '"', '"' + p1 + '"', '"' + p2 + '"', '"' + p3 + '"');});


// result
"שלום " "undefined" "ם" " " VM1494:2
"מה " "undefined" "ה" " " VM1494:2
"קורה " "undefined" "ה" " " VM1494:2
"היום" "undefined" "ם" "undefined" VM1494:2
"undefinedundefinedundefinedundefined"

edit3: I need to be able to reassemble the sentence with the same punctuation at the end.


回答1:


Well, as you might know Hebrew in the web is a bitch. Try using this regex:

[\s]*(\S)+[\s]*

example:

var words = sentence.match(/[\s]*(\S)+[\s]*/g);

It does leave in the trailing spaces to clear them up you could do something of this sort:

words = words.join().split(" ")

I'm trying out some other regex variations to try and circumvent the join-split hack, I'll update if I find anything.

Also, you could go the "replace" way and do:

var words = sentence.replace(/[#`~?!#\$%\.;:,]*/g, "").split(" ")

Just make sure to add any punctuation that might be used.

Then to get a new HTML string with the words wrapped with a span tag you can do this:

Let's say that:

var sentence = "?שלום, מה קורה מהיום"
var words = sentence.replace(/[#`~?!#\$%\.;:,]*/g, "").split(" ")

Then:

var newSentence = encodeURI(sentence)
words.forEach(function(word){
   word = encodeURI(word)
   newSentence = newSentence.replace(word, "<span>" + word + "</span>")
})
newSentence = decodeURI(newSentence);

newSentence will have your words wrapped with a span tag while leaving in the punctuations:




回答2:


try unicode between chars regexp match type of hebrew range

"בדיקה, להלהלה".match(/[\u0590-\u05F4\uFB00-\uFB4F]+/g)

by punctuation (simple):

".בדיק'ה, להלהל\"ה".match(/[^,.]+/g)

regexp for all puntuation:

"aa bb cc.בדיק'ה, להלהל\"ה .test'da, te\"xt".replace(/[\-=_!"#%&'*{},.\/:;?\(\)\[\]@\\$\^*+<>~`\u00a1\u00a7\u00b6\u00b7\u00bf\u037e\u0387\u055a-\u055f\u0589\u05c0\u05c3\u05c6\u05f3\u05f4\u0609\u060a\u060c\u060d\u061b\u061e\u061f\u066a-\u066d\u06d4\u0700-\u070d\u07f7-\u07f9\u0830-\u083e\u085e\u0964\u0965\u0970\u0af0\u0df4\u0e4f\u0e5a\u0e5b\u0f04-\u0f12\u0f14\u0f85\u0fd0-\u0fd4\u0fd9\u0fda\u104a-\u104f\u10fb\u1360-\u1368\u166d\u166e\u16eb-\u16ed\u1735\u1736\u17d4-\u17d6\u17d8-\u17da\u1800-\u1805\u1807-\u180a\u1944\u1945\u1a1e\u1a1f\u1aa0-\u1aa6\u1aa8-\u1aad\u1b5a-\u1b60\u1bfc-\u1bff\u1c3b-\u1c3f\u1c7e\u1c7f\u1cc0-\u1cc7\u1cd3\u2016\u2017\u2020-\u2027\u2030-\u2038\u203b-\u203e\u2041-\u2043\u2047-\u2051\u2053\u2055-\u205e\u2cf9-\u2cfc\u2cfe\u2cff\u2d70\u2e00\u2e01\u2e06-\u2e08\u2e0b\u2e0e-\u2e16\u2e18\u2e19\u2e1b\u2e1e\u2e1f\u2e2a-\u2e2e\u2e30-\u2e39\u3001-\u3003\u303d\u30fb\ua4fe\ua4ff\ua60d-\ua60f\ua673\ua67e\ua6f2-\ua6f7\ua874-\ua877\ua8ce\ua8cf\ua8f8-\ua8fa\ua92e\ua92f\ua95f\ua9c1-\ua9cd\ua9de\ua9df\uaa5c-\uaa5f\uaade\uaadf\uaaf0\uaaf1\uabeb\ufe10-\ufe16\ufe19\ufe30\ufe45\ufe46\ufe49-\ufe4c\ufe50-\ufe52\ufe54-\ufe57\ufe5f-\ufe61\ufe68\ufe6a\ufe6b\uff01-\uff03\uff05-\uff07\uff0a\uff0c\uff0e\uff0f\uff1a\uff1b\uff1f\uff20\uff3c\uff61\uff64\uff65]+/g," ")

actually, there are many abbreviations in Hebrew probably to include them too

".בדיק'ה, להלהל\"ה".match(/[\u0590-\u05F4\uFB00-\uFB4F][\u0590-\u05F4\uFB00-\uFB4F'"]*/g)
(2) ["בדיק'ה", "להלהל"ה"]

original phrase

"שלום, מה קורה מהיום".match(/[\u0590-\u05F4\uFB00-\uFB4F][\u0590-\u05F4\uFB00-\uFB4F'"]*/g)
(4) ["שלום", "מה", "קורה", "מהיום"]

with english

 "aa bb cc.בדיק'ה, להלהל\"ה .test'da, te\"xt".match(/[\u0590-\u05F4\uFB00-\uFB4F][\u0590-\u05F4\uFB00-\uFB4F'"]*/g)
(2) ["בדיק'ה", "להלהל"ה"]

maybe add English too (I decided there are no Hebrew style abbreviations in English)

"aa bb cc.בדיק'ה, להלהל\"ה .test'da, te\"xt".match(/[\u0590-\u05F4\uFB00-\uFB4F][\u0590-\u05F4\uFB00-\uFB4F'"]*|[a-zA-Z']+/g)
(8) ["aa", "bb", "cc", "בדיק'ה", "להלהל"ה", "test'da", "te", "xt"]



回答3:


i would do it this way:

yourString.split(' ').map(function(e){ return "<span>"+e+"</span>"; }).join('');

and make sure to add style="direction:rtl; to the wrapper element to handle punctuation.




回答4:


Just omit the matching groups from spaces.

var regex = /[\s,]*([^\s,]+)[\s,]*/g;

var sentence = "שלום, מה קורה מהיום";

var text = sentence.replace(regex, '<span>$1</span>');

console.log(text);  

// <span>שלום</span><span>מה</span><span>קורה</span><span>מהיום</span>


来源:https://stackoverflow.com/questions/21215580/identify-and-isolate-hebrew-word-with-regexp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!