简体   繁体   English

javascript regex:如何挑选英文单词,非英语字符和特殊字符?

[英]javascript regex: how to single out english words AND non-english characters AND special characters?

I'm working with string which contains both english and Chinese characters. 我正在使用包含英文和中文字符的字符串。 I want to single out each english word , non-english characters eg french, chinese etc., numbers and special characters eg "@#$%^&>?" 我想选择每个英文单词非英语字符(例如法语,中文等), 数字特殊字符(例如“ @#$%^&>?”) for further manipulation. 以便进一步操作。

So I tired 所以我累了

var nregex = /[^\u0000-\u007F]|[a-z]+|\d|[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/ig

It works for most of the case, but I'm worried some special characters or emoji not included in the list of my code. 它适用于大多数情况,但是我担心代码列表中未包含一些特殊字符或表情符号。

Is there an easier way other than list all special characters as I did? 除了像我一样列出所有特殊字符之外,还有其他更简单的方法吗?

Not the perfect solution, and you may need to tweak it, but will work for the example given: 这不是一个完美的解决方案,您可能需要对其进行调整,但将适用于以下示例:

 string2 = "I love you 我爱你" englishChars = string2.replace(/[^az ]/ig, "").trim().split(/\\s+/); nonEnglishChars = string2.replace(/[az ]/ig, "").split(/[ ]*/); final = englishChars.concat(nonEnglishChars); console.log(final); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM