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:
- split after each space ” ” (I know how)
- 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
Assuming the result should be
'&doe'and not'&#doe', a simple solution would be to just replace all.and#with&split by spaces:/\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: