Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何拆分格式为';htmlElement#idName.className.另一个className';?_Javascript_Regex - Fatal编程技术网

Javascript 如何拆分格式为';htmlElement#idName.className.另一个className';?

Javascript 如何拆分格式为';htmlElement#idName.className.另一个className';?,javascript,regex,Javascript,Regex,我有一个像header#top.header.header--show offset这样的字符串,我正努力想知道如何将它拆分为['header'、'#top'、'.header'、'.header--show offset'] 谢谢大家! 您可以使用正则表达式: let str=“header#top.header.header——显示偏移量”; //保留分隔符 设res=str.split(/(?=[#.])/gi); 控制台日志(res)我将分两步完成此操作: 将id和类符号替换为逗号,然

我有一个像
header#top.header.header--show offset
这样的字符串,我正努力想知道如何将它拆分为
['header'、'#top'、'.header'、'.header--show offset']

谢谢大家!

您可以使用正则表达式:

let str=“header#top.header.header——显示偏移量”;
//保留分隔符
设res=str.split(/(?=[#.])/gi);

控制台日志(res)我将分两步完成此操作:

  • 将id和类符号替换为逗号,然后替换为
  • 用逗号分割结果字符串
  • var selectorString=“header#top.header.header——显示偏移量”;
    selectorString=selectorString.replace(/#/g,“,#”)
    selectorString=selectorString.replace(/\./g,“,.”);
    var selectorList=selectorString.split(“,”);
    
    console.log(选择器列表)这几乎有效。他希望在拆分后将
    #
    字符保留在数组中。因此,您需要进行前瞻性拆分-
    str.split(/(?=[#.])/g)