admin管理员组文章数量:1241084
Try though I may, can't figure out how to use regex in javascript to get an array of words (assuming all words are capitalized). For example:
Given this: NowIsAGoodTime
How can you use regex to get: ['Now','Is','A','Good','Time']
Thanks Very Much!!
Try though I may, can't figure out how to use regex in javascript to get an array of words (assuming all words are capitalized). For example:
Given this: NowIsAGoodTime
How can you use regex to get: ['Now','Is','A','Good','Time']
Thanks Very Much!!
Share Improve this question asked Nov 11, 2011 at 13:27 user815460user815460 1,1433 gold badges11 silver badges17 bronze badges2 Answers
Reset to default 12'NowIsAGoodTime'.match(/[A-Z][a-z]*/g)
[A-Z]
, [a-z]
and [0-9]
are character sets defined as ranges. They could be [b-xï]
, for instance (from "b" to "x" plus "ï").
String.prototype.match
always returns an array or null
if no match at all.
Finally, the g
regexp flag stands for "global match". It means, it will try to match the same pattern on subsequent string parts. By default (with no g
flag), it will be satisfied with the first match.
With a globally matching regexp, match
returns an array of matching substrings.
With single match regexps, it would return the substring matched to the whole pattern, followed by the pattern groups matches. E. g.:
'Hello'.match(/el(lo)/) // [ 'ello', 'lo' ]
See more on Regexp
.
This regex will break it up in desired parts:
([A-Z][a-z]*)
本文标签: Use Regex in Javascript to return an array of wordsStack Overflow
版权声明:本文标题:Use Regex in Javascript to return an array of words - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740066264a2222839.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论