Javascript regular expression issue
I have more strings split by 2 spaces , something like:
abc adfdfg aefdf xcv
^^ ^^ ^^
What is the correct regular expr开发者_如何转开发ession to retrieve that strings. Thanks.
LE: what i tried is : split(/[a-zA-Z\-]\s{2}/);
and it is not working
The easiest way to break that into separate words is like this (requires exactly two spaces):
var str = "abc adfdfg aefdf xcv";
var words = str.split(" ");
words
is now an array of words from the string.
If you want to split on any amount of whitespace, you can use a regular expression as the split argument:
var str = "abc adfdfg aefdf xcv";
var words = str.split(/\s+/);
Demo here: http://jsfiddle.net/jfriend00/pkUh9/
If you want to split on two or more units of whitespace, you can use this regular expression as the split argument:
var str = "abc adfdfg aefdf xcv";
var words = str.split(/\s{2,}/);
Assume your string to be declared using var string = 'abc adfdfg aefdf xcv'
. Then use:
string = string.split(/\s{2,}/);
returns a list of substrings, split by at least two white-space characters (including tabs and newlines). If you only want to split spaces, use (space) instead of
\s
.
Just
'abc adfdfg aefdf xcv'.split(/\s+/)
精彩评论