How to pass a variable to split () in javascript

This question already has an answer here:

  • How do you use a variable in a regular expression? 16 answers

  • 使用正则表达式对象并试试这个:

    var modifiers = "gi"
    var patt = new RegExp(term,modifiers);
    var textSplit = text.split(patt);
    

    var term = /TO/gi;
    var textSplit = text.split(term);
    
    链接地址: http://www.djcxy.com/p/76838.html

    上一篇: 用正则表达式替换javascript中的函数

    下一篇: 如何在javascript中将变量传递给split()