开发者

split mail message with regular expression

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜