开发者

Splitting a string into sets of words [closed]

开发者 https://www.devze.com 2023-04-05 02:17 出处:网络
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post.
Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 9 years ago.

开发者_如何学C Improve this question

I want a regular expression for string "Occupational Health and Safety". I want the first word to be split in $1 and the rest of the string in $2. So "Occupational" in one string and "Health and Safety" in the other string. Can you please help me out?

Thanks is advance.


(\S+)\s+(.*)

puts the first non-whitespace string into $1 and the rest after the splitting whitespace into $2.

match = subject.match(/(\S+)\s+(.*)/);
if (match != null) {
    // first word: match[1]
    // rest of string: match[2]
} else {
    // Match attempt failed
}

If your string can contain newlines, you need (since you're using JavaScript):

(\S+)\s+([\s\S]*)
0

精彩评论

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

关注公众号