开发者

Split a string on capitals and digits

开发者 https://www.devze.com 2023-04-09 06:52 出处:网络
I\'m currentl using thing.replace(/([a-z])([A-Z])/g, \"$1 $2\"); to split a string on capitals like so.

I'm currentl using thing.replace(/([a-z])([A-Z])/g, "$1 $2"); to split a string on capitals like so.

HereAreSomeWords ====== Here Are Some Words

SomeMoreStuff ====== Some More Stuff

I'd like to update the regex so that开发者_运维知识库 it will split on groups of numbers as well. So the desired output would be:

123SomeWords ======== 123 Some Words

Some1Words ======= Some 1 Words

Some1234Words ======= Some 1234 Words

Does anyone have any thoughts on how to do this?


Try the pattern:

/([a-z\d](?=[A-Z])|[a-zA-Z](?=\d))/g

and replace it with this:

"$1 "

Here's a demo:

var tests = new Array(
  "HereAreSomeWords",
  "SomeMoreStuff",
  "123SomeWords",
  "Some1Words",
  "Some1234Words"
);

for (var i in tests) {
  print(tests[i] + " -> " + tests[i].replace(/([a-z\d](?=[A-Z])|[a-zA-Z](?=\d))/g, "$1 "));
}

which prints:

HereAreSomeWords -> Here Are Some Words
SomeMoreStuff -> Some More Stuff
123SomeWords -> 123 Some Words
Some1Words -> Some 1 Words
Some1234Words -> Some 1234 Words

as you can see on Ideone: http://ideone.com/KE64z


edit

Perhaps a more intuitive way would be to globally match the parts you're interested in (either numbers: \d+, or capitalized words: [A-Z][a-z]*) and join(' ') these together:

for (var i in tests) {
  print(tests[i].match(/\d+|[A-Z][a-z]*/g).join(' '));
}

which would result in the same output.

Note that my examples only take ascii letters in account: words like café would not work because of the é!

0

精彩评论

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