JavaScript Regex split by hyphen and spaces

前端 未结 2 1819
别跟我提以往
别跟我提以往 2021-01-26 05:17

I have a string in the following format: 90000 - 90000 Where the numbers can be of a variable length, and then there is a space, hyphen, space between them. I\'m tr

相关标签:
2条回答
  • 2021-01-26 06:01

    You can use split function:

    var s = '90000 - 90000';
    var a = s.split(/[ -]+/);
    console.log(a);
    

    Output:

    ["90000", "90000"]
    
    0 讨论(0)
  • 2021-01-26 06:05

    There are multiple problems with your Regex statement. First, if it is a regular expression it does not need to be enclosed in quotes. Second, you forgot the terminating / character. Third, and most importantly, regular expressions are not needed here:

    var string = "90000 - 90000";
    var array  = string.split(" - ");
    console.log(array);
    

    outputs:

    ["90000", "90000"]
    
    0 讨论(0)
提交回复
热议问题