2

Code:

var str = '/tip -a 20 send 20tips to   chinu';
var parts = str.match(/^\/(\S+)\s+(\S+)\s+(\S+)\s*(.*)/).slice(1);
console.log(parts);

Output:

["tip", "-a", "20", "send 20tips to   chinu"]

I want to change the text :

var str = '/tip 20 send 20tips to   chinu';

The result should be ["tip", "20", "send 20tips to chinu"] but i am getting ["tip", "20", "send", "20tips to chinu"]

Please help me.

EDIT:

variable str contains dynamic text. It may be /tip 20 or /tip -a 20 check http://myshowcam.com/NewSite/chat-room/chinu type /help command you will get /tip command instructions.

10
  • 2
    Again: TIP or PRICE? Commented May 5, 2015 at 7:23
  • its tip. Yesterday I was posting same question. but not getting any satisfied answer. Commented May 5, 2015 at 7:25
  • If it's dynamic, then you should not use (\S+)\s+(\S+)\s+(\S+)\s. So, by default, it matches three words Commented May 5, 2015 at 7:25
  • Because you were not giving the full story. Please refrain from posting the same question again, instead add information to your original question Commented May 5, 2015 at 7:25
  • In this code "/tip -a 20 send 20tips to chinu" you have 3 "single" words before "send 20tips to chinu", in this one "/tip 20 send 20tips to chinu", you have only 2 "words", the "-a" is missing. So the output is correct. Checkout this here regex101.com Commented May 5, 2015 at 7:27

3 Answers 3

1

You can use this regex with optional 2nd part with filter(Boolean):

// case 1
var str = '/tip -a 20 send 20tips to   chinu';
var parts = 
 (str.match(/^\/(tip)\s+(-[apm]\s+)?(\S+)\s*(.*)/) || ['tip', 'tip']).
      filter(Boolean).slice(1);
//=> ["tip", "-a ", "20", "send 20tips to   chinu"]

// case 2
str = '/tip 20 send 20tips to   chinu';
parts = 
 (str.match(/^\/(tip)\s+(-[apm]\s+)?(\S+)\s*(.*)/) || ['tip', 'tip']).
      filter(Boolean).slice(1);
//=> ["tip", "20", "send 20tips to   chinu"]

// case 3
str = '/tip';
parts = 
 (str.match(/^\/(tip)\s+(-[apm]\s+)?(\S+)\s*(.*)/) || ['tip', 'tip']).
      filter(Boolean).slice(1);
//=> ["tip"]
Sign up to request clarification or add additional context in comments.

9 Comments

var newStr = str.match(/^\/(\S+)\s+(-[apm]\s+)?(\S+)\s*(.*)/).filter(Boolean).slice(1); I have just modified your code. Now I am typing only /tip without any price, type and message. Still I am getting error.
I am using above input string which you have written. If i am entering only /tip i want to validate. I have used your code. But i am getting TypeError: str.match(...) is null error
You are right. But i was saying to validate. This is a text field. User may put any word. So i need to validate. If there is no word after /tip it says Error: Enter your tips amount if there are /tip -a it says Error: please enter valid number. I have tried yesterday. Unable to complete. I am very week in regular expression. Please help me. Thanks
My tips format is - /tip [optional -a,-p,-m] 20 [optional_comments] or /tip 20 [optional_comments]
Yes starting with /tip in other case it will change. Please check myshowcam.com/NewSite/chat-room/chinu type /help you will able to see the commands
|
1

    var str1 = '/tip -a 20 send 20tips to   chinu';
    var str2 = '/tip 20 send 20tips to   chinu';
    var str3 = '/tip 20';
    
    function getArr(str) {
        if (str.split(" ")[1].indexOf("-")==0) return str.match(/^\/(\S+)\s+(\S+)\s+(\S+)\s*(.*)/).slice(1);
        else return str.match(/^\/(\S+)\s+(\S+)\s*(.*)/).slice(1);
    }
    alert(getArr(str1))
    alert(getArr(str2))
    alert(getArr(str3))

5 Comments

Hi, Your code is working fine. But I am getting error when type only /tip If I will typing /tip it will saying Error: Please enter tips amount
Error - TypeError: str.split(...)[1] is undefined if (str.split(" ")[1].indexOf("-")==0) return str.match(/^\/(\S+)\s+(\S+)\s+(\S+...
Two cases this is not working 1st /tip and 2nd are /tip -any_word
They work. try test. If the strings are wrong, give me more examples
I do not understand what you enter to get errors. It should not be possible to enter /tip only - it must be followed by an amount. no?
0

You can use the following if your dynamic text always contains - before optional switch

^\/(\S+)\s+(-\S+)?\s*(\S+)\s*(.*)

Comments

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.