简体   繁体   English

Javascript正则表达式 - 拆分字符串

[英]Javascript regex - split string

Struggling with a regex requirement. 苦苦挣扎的正则表达式要求。 I need to split a string into an array wherever it finds a forward slash. 我需要将字符串拆分成数组,无论它在哪里找到正斜杠。 But not if the forward slash is preceded by an escape. 但是如果正斜杠之前是逃避的话。

Eg, if I have this string: 例如,如果我有这个字符串:

hello/world

I would like it to be split into an array like so: 我希望将它分成如下数组:

arrayName[0] = hello
arrayName[1] = world

And if I have this string: 如果我有这个字符串:

hello/wo\/rld

I would like it to be split into an array like so: 我希望将它分成如下数组:

arrayName[0] = hello
arrayName[1] = wo/rld

Any ideas? 有任何想法吗?

I wouldn't use split() for this job. 我不会使用split()来完成这项工作。 It's much easier to match the path components themselves, rather than the delimiters. 匹配路径组件本身,而不是分隔符更容易。 For example: 例如:

var subject = 'hello/wo\\/rld';
var regex = /(?:[^\/\\]+|\\.)+/g;
var matched = null;
while (matched = regex.exec(subject)) {
  print(matched[0]);
}

output: 输出:

hello
wo\/rld

test it at ideone.com 在ideone.com上测试一下

Here's a way adapted from the techniques in this blog post : 以下是根据此博客文章中的技术改编的方式:

var str = "Testing/one\\/two\\/three";
var result = str.replace(/(\\)?\//g, function($0, $1){
  return $1 ? '/' : '[****]';
}).split('[****]');

Live example 实例

Given: 鉴于:

Testing/one\/two\/three

The result is: 结果是:

[0]: Testing
[1]: one/two/three

That first uses the simple "fake" lookbehind to replace / with [****] and to replace \\/ with / , then splits on the [****] value. 首先使用简单的“假”lookbehind替换/ [****]并替换\\/ with / ,然后拆分[****]值。 (Obviously, replace [****] with anything that won't be in the string.) (显然,将[****]替换为不在字符串中的任何内容。)

The following is a little long-winded but will work, and avoids the problem with IE's broken split implementation by not using a regular expression. 以下是一个有点啰嗦,但会工作,并通过不使用正则表达式避免IE的分裂实现的问题。

function splitPath(str) {
    var rawParts = str.split("/"), parts = [];
    for (var i = 0, len = rawParts.length, part; i < len; ++i) {
        part = "";
        while (rawParts[i].slice(-1) == "\\") {
            part += rawParts[i++].slice(0, -1) + "/";
        }
        parts.push(part + rawParts[i]);
    }
    return parts;
}

var str = "hello/world\\/foo/bar";
alert( splitPath(str).join(",") );

/* If you are getting your string from an ajax response or a data base query, that is, the string has not been interpreted by javascript, you can match character sequences that either have no slash or have escaped slashes. / *如果从ajax响应或数据库查询中获取字符串,也就是说,字符串未被javascript解释,则可以匹配没有斜杠或具有转义斜杠的字符序列。 If you are defining the string in a script, escape the escapes and strip them after the match. 如果要在脚本中定义字符串,请转义转义并在匹配后删除它们。 */ * /

var s='hello/wor\\/ld';
s=s.match(/(([^\/]*(\\\/)+)([^\/]*)+|([^\/]+))/g) || [s];
alert(s.join('\n'))
s.join('\n').replace(/\\/g,'')

/*  returned value: (String)
hello
wor/ld
*/

这是rubular.com的一个例子

For short code, you can use reverse to simulate negative lookbehind 对于短代码,您可以使用反向来模拟负向后视

function reverse(s){
  return s.split('').reverse().join('');
}

var parts = reverse(myString).split(/[/](?!\\(?:\\\\)*(?:[^\\]|$))/g).reverse();
for (var i = parts.length; --i >= 0;) { parts[i] = reverse(parts[i]); }

but to be efficient, it's probably better to split on /[/]/ and then walk the array and rejoin elements that have an escape at the end. 但为了提高效率,最好在/[/]/上拆分,然后遍历数组并重新加入在末尾有转义的元素。

Something like this may take care of it for you. 这样的事可能会照顾你。

var str = "/hello/wo\\/rld/";
var split = str.replace(/^\/|\\?\/|\/$/g, function(match) {
  if (match.indexOf('\\') == -1) {
    return '\x00';
  }
  return match;
}).split('\x00');       

alert(split);

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

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