最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to Split string with multiple rules in javascript - Stack Overflow

programmeradmin1浏览0评论

I have this string for example:

str = "my name is john#doe oh.yeh";

the end result I am seeking is this Array:

strArr = ['my','name','is','john','&#doe','oh','&yeh'];

which means 2 rules apply:

  1. split after each space " " (I know how)
  2. if there are special characters ("." or "#") then also split but add the characther "&" before the word with the special character.

I know I can strArr = str.split(" ") for the first rule. but how do I do the other trick?

thanks, Alon

I have this string for example:

str = "my name is john#doe oh.yeh";

the end result I am seeking is this Array:

strArr = ['my','name','is','john','&#doe','oh','&yeh'];

which means 2 rules apply:

  1. split after each space " " (I know how)
  2. if there are special characters ("." or "#") then also split but add the characther "&" before the word with the special character.

I know I can strArr = str.split(" ") for the first rule. but how do I do the other trick?

thanks, Alon

Share Improve this question asked Feb 19, 2012 at 18:29 AlonAlon 7,75820 gold badges64 silver badges100 bronze badges 6
  • 2 Should it be '&#doe' or '&doe'? You have '&yeh' and not '&.yeh'. – Felix Kling Commented Feb 19, 2012 at 18:30
  • Do you expect to have those "special characters" as part of the input or there's no way any of them will occur even once? – Nitzan Tomer Commented Feb 19, 2012 at 18:31
  • possible duplicate of How do I split a string that contains different signs? – outis Commented Feb 19, 2012 at 18:35
  • @outis Did you miss the second part? – Rob W Commented Feb 19, 2012 at 18:39
  • @RobW: no, but this is really two questions, the first of which has already been asked. Currently looking for another of the second. – outis Commented Feb 19, 2012 at 18:42
 |  Show 1 more ment

5 Answers 5

Reset to default 6

Assuming the result should be '&doe' and not '&#doe', a simple solution would be to just replace all . and # with & split by spaces:

strArr = str.replace(/[.#]/g, ' &').split(/\s+/)

/\s+/ matches consecutive white spaces instead of just one.

If the result should be '&#doe' and '&.yeah' use the same regex and add a capture:

strArr = str.replace(/([.#])/g, ' &$1').split(/\s+/)

You have to use a Regular expression, to match all special characters at once. By "special", I assume that you mean "no letters".

var pattern = /([^ a-z]?)[a-z]+/gi;             // Pattern
var str = "my name is john#doe oh.yeh";         // Input string
var strArr = [], match;                         // output array,  temporary var
while ((match = pattern.exec(str)) !== null) {  // <-- For each match
   strArr.push( (match[1]?'&':'') + match[0]);  // <-- Add to array
}
// strArr is now:
// strArr = ['my', 'name', 'is', 'john', '&#doe', 'oh', '&.yeh']

It does not match consecutive special characters. The pattern has to be modified for that. Eg, if you want to include all consecutive characters, use ([^ a-z]+?).

Also, it does nothing include a last special character. If you want to include this one as well, use [a-z]* and remove !== null.

use split() method. That's what you need: http://www.w3schools./jsref/jsref_split.asp

Ok. i saw, you found it, i think:

1) first use split to the whitespaces
2) iterate through your array, split again in array members when you find # or .
3) iterate through your array again and str.replace("#", "&#") and str.replace(".","&.") when you find

I would think a bination of split() and replace() is what you are looking for:

str = "my name is john#doe oh.yeh";

strArr = str.replace('\W',' &');

strArr = strArr.split(' '); 

That should be close to what you asked for.

This works:

array = string.replace(/#|\./g, ' &$&').split(' ');

Take a look at demo here: http://jsfiddle/M6fQ7/1/

发布评论

评论列表(0)

  1. 暂无评论