kulebyashik
kulebyashik

Reputation: 5619

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 to learn it by myself but i've read alot of articles but still no ideas.

p.s. using regex+javascript

Upvotes: 0

Views: 255

Answers (1)

Felix Kling
Felix Kling

Reputation: 816780

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.

Upvotes: 1

Related Questions