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.