admin管理员组

文章数量:1173665

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?

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?

Share Improve this question asked Jan 31, 2011 at 16:10 WastedSpaceWastedSpace 1,1437 gold badges22 silver badges34 bronze badges 1
  • This page will probably help: blog.stevenlevithan.com/archives/mimic-lookbehind-javascript Not posting this as an answer because I don't have time right now to come up with an example specific to what you need. But I'm quite certain you can get there from that post. Good luck. – T.J. Crowder Commented Jan 31, 2011 at 16:15
Add a comment  | 

7 Answers 7

Reset to default 23

I wouldn't use split() for this job. 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

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.

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(",") );

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. (Obviously, replace [****] with anything that won't be in the string.)

/* 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. 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
*/

Here's an example at 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);

本文标签: Javascript regexsplit stringStack Overflow