Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
开发者_如何学C Improve this questionI 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]*)
精彩评论