如何在保留引用字符串的同时拆分此行>div#a.more.style.ui[url="in.tray"]{value}
拆分的字符在哪里>
#
.
[
{
产生:
>div
#a
.more
.style
.ui
[url="in.tray"]
{value}
目前的努力是:
\>|\[|\{|#|\.?(?:(["'])(?:\\?.)*?\1)*
"in.tray"
被拆分。更新 1:
该解决方案需要基于正则表达式,因为模式是由现有代码中 JS 对象的键组合而成的,它们是:
JSObject
'>': function ...
'^': function ...
'[': function ...
...
将函数用作回调来处理正则表达式的输出。
目标字符串是一个 Emmet 宏,可能包含要开始的纯字符,以及至少
^
和 $
的可能重复,被视为单独的元素,例如:p>div>div>span^h2^^h1>div#a.li^mo+re.st*yle.ui[url="in.tray"]{value}$$$
当前的工作基于 @tim-pietzcker 使用
.match()
但过滤掉了空的最后一场比赛:[a-z$^+*>#.[{]{0,1}(?:"[^"]*"|[^"$^+*>#.[{]){0,}
最佳答案
不要使用 split()
,然后很简单:
result = subject.match(/[>#.[{](?:"[^"]*"|[^">#.[{])+/g);
看到它 live on regex101.com 。
解释:
[>#.[{] # Match a "splitting" character
(?: # Start of group to match either...
"[^"]*" # a quoted string
| # or
[^">#.[{] # any character except quotes and "splitting" characters
)+ # Repeat at least once.
关于javascript - 正则表达式在引号外的特定字符上拆分字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22503197/