Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
16
rated 0 times [  18] [ 2]  / answers: 1 / hits: 25443  / 13 Years ago, sat, may 28, 2011, 12:00:00

I have some random string, for example: Hello, my name is john.. I want that string split into an array like this: Hello, ,, , my, name, is, john, .,. I tried str.split(/[^ws]|_/g), but it does not seem to work. Any ideas?


More From » regex

 Answers
10

Try this (I'm not sure if this is what you wanted):



str.replace(/[^ws]|_/g, function ($1) { return ' ' + $1 + ' ';}).replace(/[ ]+/g, ' ').split(' ');


http://jsfiddle.net/zNHJW/3/


[#92006] Thursday, May 26, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
trayvon

Total Points: 35
Total Questions: 117
Total Answers: 88

Location: Guernsey
Member since Tue, Jul 6, 2021
3 Years ago
;