开发者

split mail message with regular expression

开发者 https://www.devze.com 2023-03-09 18:50 出处:网络
I want to split incoming mail messages into word array. smth like this: \"Hi,.***Build,son,!8loop\" into

I want to split incoming mail messages into word array. smth like this:

"Hi,.***Build, son,!8loop"

into

['Hi' , 'Build', 'son', 'loop']

i know you want to send me t开发者_运维知识库o learn it by myself but i've read alot of articles but still no ideas.

p.s. using regex+javascript


You can do the following:

  1. Replace all non-word characters by a white space:

    str = str.replace(/[\W\d\s]+/g, ' ');
    

    You might have to adjust this to your needs, to replace the right characters:

    • \W matches everything expect letters, digits, and underscores
    • \d matches digits
    • \s matches white spaces

    There are other ways to write an equivalent expression.

  2. Then split bye white space:

    words = str.split(' ');
    

Regular-Expressions.info is a good website to learn about regular expressions. For regular expressions in JavaScript, have a look at the MDC documentation.

0

精彩评论

暂无评论...
验证码 换一张
取 消