0

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.

4
  • someone asked the similar problem. What you need is split them into array and join the last few items using " " Commented 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. Commented 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. Commented May 4, 2015 at 11:49
  • /PRICE OR /TIP - Come ON!!!???? Commented May 4, 2015 at 11:58

2 Answers 2

5

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

  1. the string you get might be wrong,
  2. you want to ensure the third part is a number,
  3. the parameter -a or -e or -something is optional,
  4. 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", ""]

Sign up to request clarification or add additional context in comments.

10 Comments

But price type is optional.
how do you know the exact format of str?? the one he has mentioned is just an example! But nice one, really liked the regular expression!
your regex definitely works there is no doubt abt it! but without him mentioning any conditions how can you be sure of it!
This is working fine but this case is showing wrong output /price 20 sending USD 20 to dystroy :)
@chatfun I didn't knew the second argument (like -a) was optional
|
0

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.
good morning @chatfun, I guess you'll have to update you're answer with several examples you need to match.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.