JavaScript 分割字符串
JavaScript split a string
请看我的JavaScript代码:
var str = "/price -a 20 tips for model";
var removeSlsh = str.slice(1); //output = price -a 20 tips for model
var newStr = removeSlsh.replace(/\s+/g,' ').trim().split(' ');
console.log(newStr); // Output = ["price", "-a", "20", "tips", "for", "model"]
以上代码运行良好。每个字符串拆分具有 space。但我需要像
这样拆分上面的字符串
1 = price // Field name
2 = -a // price type -a anonymous, -m model, -p private
3 = 20 // amount of price
4 = tips for model //comment
输出应该是
["price", "-a", "20", "tips for model"]
编辑:
如果我设置拆分文本的限制。看起来像
var newStr = removeSlsh.replace(/\s+/g,' ').trim().split(' ',4);
console.log(newStr); // Output = ["price", "-a", "20", "tips"]
N.B - 价格类型和评论是可选字段。字符串可以是 /price 20
或 /price 20 tips for model
或 /price -a 20
但价格字段是强制性的并且必须是数值。第二个字段是可选的,您不会输入任何值。如果您将输入 -a, -m, -p
以外的任何文本,则此字段有效。
你不需要拆分,但提取部分,这可以用正则表达式来完成:
var parts = str.match(/^\/(\S+)\s+(\S+)\s+(\S+)\s*(.*)/).slice(1);
结果:
["price", "-a", "20", "tips for model"]
现在假设
- 您得到的字符串可能有误,
- 你要确保第三部分是数字,
- 参数-a或-e或-something是可选的,
- 最后一部分(评论)是可选的,
那么你可以使用这个:
var m = str.match(/^\/(\S+)(\s+-\w+)?\s+(-?\d+\.?\d*)\s*(.*)/);
if (m) {
// OK
var parts = m.slice(1); // do you really need this array ?
var fieldName = m[1]; // example: "price"
var priceType = m[2]; // example: "-a" or undefined
var price = +m[3]; // example: -23.41
va comments = m[4]; // example: "some comments"
...
} else {
// NOT OK
}
示例:
"/price -20 some comments"
给出 ["price", undefined, "-20", "some comments"]
"/price -a 33.12"
给出 ["price", "-a", "33.12", ""]
var str = "/price -a 20 tips for model";
str = str.replace(/\//g,'').replace(/\s+/g,' '); //removes / and multiple spaces
var myregexp = /(.*?) (.*?) (.*?) (.*)/mg; // the regex
var match = myregexp.exec(str).slice(1); // execute the regex and slice the first match
alert(match)
输出:
["price", "-a", "20", "tips for model"]
请看我的JavaScript代码:
var str = "/price -a 20 tips for model";
var removeSlsh = str.slice(1); //output = price -a 20 tips for model
var newStr = removeSlsh.replace(/\s+/g,' ').trim().split(' ');
console.log(newStr); // Output = ["price", "-a", "20", "tips", "for", "model"]
以上代码运行良好。每个字符串拆分具有 space。但我需要像
这样拆分上面的字符串1 = price // Field name
2 = -a // price type -a anonymous, -m model, -p private
3 = 20 // amount of price
4 = tips for model //comment
输出应该是
["price", "-a", "20", "tips for model"]
编辑:
如果我设置拆分文本的限制。看起来像
var newStr = removeSlsh.replace(/\s+/g,' ').trim().split(' ',4);
console.log(newStr); // Output = ["price", "-a", "20", "tips"]
N.B - 价格类型和评论是可选字段。字符串可以是 /price 20
或 /price 20 tips for model
或 /price -a 20
但价格字段是强制性的并且必须是数值。第二个字段是可选的,您不会输入任何值。如果您将输入 -a, -m, -p
以外的任何文本,则此字段有效。
你不需要拆分,但提取部分,这可以用正则表达式来完成:
var parts = str.match(/^\/(\S+)\s+(\S+)\s+(\S+)\s*(.*)/).slice(1);
结果:
["price", "-a", "20", "tips for model"]
现在假设
- 您得到的字符串可能有误,
- 你要确保第三部分是数字,
- 参数-a或-e或-something是可选的,
- 最后一部分(评论)是可选的,
那么你可以使用这个:
var m = str.match(/^\/(\S+)(\s+-\w+)?\s+(-?\d+\.?\d*)\s*(.*)/);
if (m) {
// OK
var parts = m.slice(1); // do you really need this array ?
var fieldName = m[1]; // example: "price"
var priceType = m[2]; // example: "-a" or undefined
var price = +m[3]; // example: -23.41
va comments = m[4]; // example: "some comments"
...
} else {
// NOT OK
}
示例:
"/price -20 some comments"
给出["price", undefined, "-20", "some comments"]
"/price -a 33.12"
给出["price", "-a", "33.12", ""]
var str = "/price -a 20 tips for model";
str = str.replace(/\//g,'').replace(/\s+/g,' '); //removes / and multiple spaces
var myregexp = /(.*?) (.*?) (.*?) (.*)/mg; // the regex
var match = myregexp.exec(str).slice(1); // execute the regex and slice the first match
alert(match)
输出:
["price", "-a", "20", "tips for model"]