[英]Javascript - match word by regex - Non-English
Need to match words by checking comma between values.需要通过检查值之间的逗号来匹配单词。 Below script works fine for English text as expected.
下面的脚本按预期适用于英文文本。
var str = "Sandwich Islands,Congo, Democratic Republic,Mauritania,Finland"; var res = str.match(/\\w.*?\\w(?=,\\w|$)/g); console.log(res)
But, it doesnt work for non-english characters.. similar logic i need to bring non-english texts as well.但是,它不适用于非英文字符。类似的逻辑我也需要带上非英文文本。
You can do this using character matching您可以使用字符匹配来做到这一点
var res = str.match(/[毎日2名様までの,ボトルワインやチ2名様までご滞在中の,またはラウアメニテ]+/g);
or using character ranges, like或使用字符范围,例如
var res = str.match(/[a-zA-Z]+/g);
You can use ascii code range and base your regex on it您可以使用 ascii 代码范围并将您的正则表达式基于它
console.log("毎".charCodeAt()) console.log("A".charCodeAt()) console.log("Z".charCodeAt()) console.log("--------"); const s = "毎日2名様までの,ボトルワインやチ, (2名様まで),ご滞在中の,またはラウ,アメニテ"; [...s].map(l => { console.log(l.charCodeAt()) })
If you are positive that:如果你是肯定的:
You can try without regex:您可以尝试不使用正则表达式:
replaceAll = (what, withWhat, str) => {
const reg = new RegExp(what, 'g');
return str.replace(reg, withWhat);
}
var str = "Complimentary daily, buffet breakfast,Special amenities, wine and chocolates,two persons at selected hotels ,Food and beverage";
var res = replaceAll(", ",">", str).split(",").map(i=> replaceAll(">", ", ", i))
console.log(res)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.