简体   繁体   English

使用正则表达式在JavaScript中拆分字符串

[英]Split string in JavaScript using a regular expression

I'm trying to write a regex for use in javascript. 我正在尝试编写正则表达式以在javascript中使用。

var script = "function onclick() {loadArea('areaog_og_group_og_consumedservice', '\x26roleOrd\x3d1');}";
var match = new RegExp("'[^']*(\\.[^']*)*'").exec(script);

I would like split to contain two elements: 我希望split包含两个元素:

match[0] == "'areaog_og_group_og_consumedservice'";

match[1] == "'\x26roleOrd\x3d1'";

This regex matches correctly when testing it at gskinner.com/RegExr/ but it does not work in my Javascript. 在gskinner.com/RegExr/上测试时,此正则表达式正确匹配,但在我的Javascript中不起作用。 This issue can be replicated by testing ir here http://www.regextester.com/ . 此问题可以通过在http://www.regextester.com/上进行测试来复制。

I need the solution to work with Internet Explorer 6 and above. 我需要使用Internet Explorer 6及更高版本的解决方案。

Can any regex guru's help? 正规表达式专家可以提供帮助吗?

Judging by your regex, it looks like you're trying to match a single-quoted string that may contain escaped quotes. 从您的正则表达式判断,您似乎正在尝试匹配可能包含转义引号的单引号字符串。 The correct form of that regex is: 该正则表达式的正确形式为:

'[^'\\]*(?:\\.[^'\\]*)*'

(If you don't need to allow for escaped quotes, /'[^']*'/ is all you need.) You also have to set the g flag if you want to get both strings. (如果您不需要允许转义引号, /'[^']*'/是你所需要的。)您也可以设置g标志,如果你想获得两个字符串。 Here's the regex in its regex-literal form: 这是正则表达式形式的正则表达式:

/'[^'\\]*(?:\\.[^'\\]*)*'/g

If you use the RegExp constructor instead of a regex literal, you have to double-escape the backslashes: once for the string literal and once for the regex. 如果使用RegExp构造函数而不是正则表达式文字,则必须两次转义反斜杠:一次用于字符串文字,一次用于正则表达式。 You also have to pass the flags ( g , i , m ) as a separate parameter: 您还必须将标志( gim )作为单独的参数传递:

var rgx = new RegExp("'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'", "g");
while (result = rgx.exec(script))
    print(result[0]);

也许这样:

'([^']*)'\s*,\s*'([^']*)'

The regex you're looking for is .*?('[^']*')\\s*,\\s*('[^']*') . 您要查找的正则表达式是.*?('[^']*')\\s*,\\s*('[^']*') The catch here is that, as usual, match[0] is the entire matched text (this is very normal) so it's not particularly useful to you. 这里的问题是,与往常一样, match[0]是整个匹配的文本(这是很正常的),因此对您不是特别有用。 match[1] and match[2] are the two matches you're looking for. match[1]match[2]是您要寻找的两个匹配项。

var script = "function onclick() {loadArea('areaog_og_group_og_consumedservice', '\x26roleOrd\x3d1');}";
var parameters = /.*?('[^']*')\s*,\s*('[^']*')/.exec(script);
alert("you've done: loadArea("+parameters[1]+", "+parameters[2]+");");

The only issue I have with this is that it's somewhat inflexible. 我对此唯一的问题是它有些不灵活。 You might want to spend a little time to match function calls with 2 or 3 parameters? 您可能想花一点时间来匹配具有2个或3个参数的函数调用?

EDIT In response to you're request, here is the regex to match 1,2,3,...,n parameters. 编辑根据您的要求,这里是匹配1,2,3,...,n参数的正则表达式。 If you notice, I used a non-capturing group (the (?: ) part) to find many instances of the comma followed by the second parameter. 如果您注意到了,我使用了一个非捕获组( (?: ) :)部分)来查找逗号的许多实例,后跟第二个参数。

/.*?('[^']*')(?:\s*,\s*('[^']*'))*/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM