正则表达式在引号外的特定字符上拆分字符串 [英] Regex split string on specific chars outside quotes

查看:168
本文介绍了正则表达式在引号外的特定字符上拆分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在保留引用字符串的同时拆分此行

How can this line be split while preserving quoted strings

> div#a.more.style.ui [url =in .tray] {value}

其中分割的字符是

> [ {

收益:

>div
#a
.more
.style
.ui
[url="in.tray"]
{value}

目前的努力是:

\>|\[|\{|#|\.?(?:(["'])(?:\\?.)*?\1)*

in.tray被拆分。

更新1:

解决方案需要基于正则表达式,因为模式是从现有代码中JS对象的键组装而成的:

The solution needs to be regex based as the pattern is assembled from the keys of a JS object in the existing code, which are:

JSObject
    '>': function ...
    '^': function ...
    '[': function ...
     ...

使用函数作为回调来处理正则表达式的输出。

with the functions used as callbacks to process the output from the regex.

目标字符串是一个Emmet宏,可能包含要启动的普通字符,以及至少 ^ $ <的可能重复次数/ code>被视为单独的元素,例如:

The target string is an Emmet macro and may contain plain characters to start, as well as possible repeats of at least ^, $ to be treated as separate elements e.g:

p> div> div> span ^ h2 ^^ h1> div# a.li ^ mo + re.st * yle.ui [url =in.tray] {value} $$$

目前基于 @ tim-pietzcker 使用 .match()但过滤掉了空的最后一场比赛:

Current effort based on @tim-pietzcker using .match() but with an empty last match filtered out:

[az $ ^ + *>#。[{] {0,1} (?:[^] *| [^$ ^ + *>#。[{]){0,}

推荐答案

不要使用 split(),这很容易:

result = subject.match(/[>#.[{](?:"[^"]*"|[^">#.[{])+/g);

查看在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.

这篇关于正则表达式在引号外的特定字符上拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆