Check if string ends with

后端 未结 2 1289
说谎
说谎 2021-01-25 05:34

I want to check if a string ends with

- v{number}

So for example

hello world           false
hello world - v2      true
hello w         


        
2条回答
  •  醉话见心
    2021-01-25 06:04

    Just use this without checking anything else in the beginning

    - v\d+$
    

    This way you make sure it ends with - v followed by at least one digit. See it live in https://regex101.com/r/pB6vP5/1

    Then, your expression needs to be:

    var patt = new RegExp("- v\\d+$");
    

    As stated by anubhava in another answer:

    • No need for /
    • Requires double escaping for \d.

    var str = 'hello world - v15';
    var patt = new RegExp("- v\\d+$");
    var res = patt.test(str);
    console.log(res);

提交回复
热议问题