Javascript split regex question

后端 未结 7 2077
小鲜肉
小鲜肉 2020-11-29 21:11

hello I am trying what I thought would be a rather easy regex in Javascript but is giving me lots of trouble. I want the ability to split a date via javascript splitting ei

7条回答
  •  臣服心动
    2020-11-29 21:43

    Say your string is:

    let str = `word1
    word2;word3,word4,word5;word7
    word8,word9;word10`;
    

    You want to split the string by the following delimiters:

    • Colon
    • Semicolon
    • New line

    You could split the string like this:

    let rawElements = str.split(new RegExp('[,;\n]', 'g'));
    

    Finally, you may need to trim the elements in the array:

    let elements = rawElements.map(element => element.trim());
    

提交回复
热议问题