Please see my JavaScript code:
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"]
Above code working fine. Every string split which has space. But I need to split above string like
1 = price // Field name
2 = -a // price type -a anonymous, -m model, -p private
3 = 20 // amount of price
4 = tips for model //comment
Output should be
["price", "-a", "20", "tips for model"]
EDIT:
If i set limit of the split text. It looks like
var newStr = removeSlsh.replace(/\s+/g,' ').trim().split(' ',4);
console.log(newStr); // Output = ["price", "-a", "20", "tips"]
N.B - Price type and comments are optional field. string may be /price 20 or /price 20 tips for model or /price -a 20 but price field is mandatory and must be a numeric value. Second field is optional it you will not entering any value. If you will entering any text except -a, -m, -p then this field validate.
-
someone asked the similar problem. What you need is split them into array and join the last few items using " "Surely– Surely2015年05月04日 11:48:36 +00:00Commented May 4, 2015 at 11:48
-
If your first 3 parameters are always fieldname, flag and value you could just split out the rest of the array and use .join(' ') on it to extract the message. If you have more formats, we'd need some more information.Robin– Robin2015年05月04日 11:48:47 +00:00Commented May 4, 2015 at 11:48
-
How to join last few item? this is dynamic. I don't know comment is coming or not. comment is a optional field.Developer– Developer2015年05月04日 11:49:35 +00:00Commented May 4, 2015 at 11:49
-
/PRICE OR /TIP - Come ON!!!????mplungjan– mplungjan2015年05月04日 11:58:31 +00:00Commented May 4, 2015 at 11:58
2 Answers 2
You don't need to split, but to extract parts, which can be done with a regular expression:
var parts = str.match(/^\/(\S+)\s+(\S+)\s+(\S+)\s*(.*)/).slice(1);
Result:
["price", "-a", "20", "tips for model"]
Now suppose that
- the string you get might be wrong,
- you want to ensure the third part is a number,
- the parameter -a or -e or -something is optional,
- the last part (the comments) is optional,
then you may use this:
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
}
Examples:
"/price -20 some comments"gives["price", undefined, "-20", "some comments"]"/price -a 33.12"gives["price", "-a", "33.12", ""]
10 Comments
/price 20 sending USD 20 to dystroy :)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)
Output:
["price", "-a", "20", "tips for model"]
2 Comments
/price 20 good morning Pedro Lobito its not giving proper result. Check this.