admin管理员组文章数量:1307007
I want to split a string based on multiple delimiters.
How to split a string with multiple strings as separator?
For example:
I have a string: "/create #channel 'name' 'description of the channel' #field1 #field2"
And I want an array with:
/create
#channel
name
description of the channel
#field1
#field2
Another example, i have: "/send @user 'A messsage'"
And I want:
/send
@user
A message
How to solve it? Any help, please? :-(
I want to split a string based on multiple delimiters.
How to split a string with multiple strings as separator?
For example:
I have a string: "/create #channel 'name' 'description of the channel' #field1 #field2"
And I want an array with:
/create
#channel
name
description of the channel
#field1
#field2
Another example, i have: "/send @user 'A messsage'"
And I want:
/send
@user
A message
How to solve it? Any help, please? :-(
Share Improve this question edited Sep 24, 2015 at 8:30 Eusthace asked Sep 24, 2015 at 5:45 EusthaceEusthace 3,8617 gold badges36 silver badges43 bronze badges 6- here's a possible solution: create a for loop starting from 0 to the length of the string - 1 to make a temporary array that contains the INDICES of every char that has a delimiter that you care about. (@, /, ', etc...) Then use another for loop to make a new array with substrings of the initial string with parameters based on the indices from this temp array you created. Hope this helps. – Kabir Peshawaria Commented Sep 24, 2015 at 5:52
- i posted a jsfiddle in my answer that does it with regex, but i would say that parsing it like in @ShailendraSharma 's answer is probably better and more secure – x4rf41 Commented Sep 24, 2015 at 5:59
-
How about splitting the string by space and replacing the removing the occurrence of
'
from every item of the split result array. – Gaurav Gupta Commented Sep 24, 2015 at 6:11 -
@GauravGupta That wont work for strings like
'description of the channel'
– Tushar Commented Sep 24, 2015 at 6:13 - @Tushar That's right. I missed that totally. – Gaurav Gupta Commented Sep 24, 2015 at 6:14
5 Answers
Reset to default 3here the solution without Regx
var multiSplit = function (str, delimeters) {
var result = [str];
if (typeof (delimeters) == 'string')
delimeters = [delimeters];
while (delimeters.length > 0) {
for (var i = 0; i < result.length; i++) {
var tempSplit = result[i].split(delimeters[0]);
result = result.slice(0, i).concat(tempSplit).concat(result.slice(i + 1));
}
delimeters.shift();
}
return result;
}
simply use
multiSplit("/create #channel 'name' 'description of the channel' #field1 #field2",['/','#','@',"'"])
output
Array [ "", "create ", "channel ", "name", " ", "description of the channel", " ", "field1 ", "field2" ]
You can use regex
/([\/#]\w+|'[\s\w]+')/g
For regex explanation: https://regex101./r/lE4rJ7/3
[\/#@]\w+
: This will match the strings that start with#
,/
or@
|
: OR condition'[\s\w]+'
: Matches the strings that are wrapped in quotes
As the regex will also match the quotes, they need to be removed.
var regex = /([\/#@]\w+|'[\s\w]+')/g;
function splitString(str) {
return str.match(regex).join().replace(/'/g, '').split(',');
}
var str1 = "/create #channel 'name' 'description of the channel' #field1 #field2 @Tushar";
var str2 = "/send @user 'A messsage'";
var res1 = splitString(str1);
var res2 = splitString(str2);
console.log(res1);
console.log(res2);
document.write(res1);
document.write('<br /><br />' + res2);
this one works for your case, but not with split:
('[^']+'|[^\s']+)
note: you will still have to trim the single-quotes!
here is an example: http://jsfiddle/k8s8r77e/1/
but i can't tell you if it will always work. Parsing in that way is not really what regex is for.
Something like this might work.. but would have to be tweaked to not wipe out apostrophes
var w = "/create #channel 'name' 'description of the channel' #field1 #field2";
var ar = w.split();
ar.forEach(function(e, i){
e = e.replace(/'/g, '')
console.log(e)
})
fiddle: http://jsfiddle/d9m9o6ao/
There is something inconsistent in the example you gave, you either split by those fields or give them as fields markers, but you seem to mix. Take you first example, in the original string you have 'name', but in the result you strip it of the ' and have only name, while #channel keeps the #. Anyways you have 2 options:
String.split takes a regular expression so you can use:
var re = /['\/@#"]+\s*['\/@#"]*/;
var str = "/create #channel 'name' 'description of the channel' #field1 #field2";
var parts = str.split(re);
You get an array of your tokens. Note the first one in this case is empty, just remove empty strings before using the parts array.
Or, use string.match with a regexp:
var re = /['\/@#"][^'\/@#"]*/g;
var parts = str.match(re);
本文标签: javascriptSplit a string based on multiple delimiters 3939Stack Overflow
版权声明:本文标题:javascript - Split a string based on multiple delimiters [, #, @, ''] - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741833058a2400050.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论