1

I'm using the javascript's method "split()" to split a string at '\' or '/' characters. I searched some other posts to see how regular expressions work and tried this:

var text1 = "some/text";
var text2 = "some\\text2"
var words = text1.split(/\//);
var words2 = text2.split(/\\/);

Both work great, but When I try this one:

var text3 = "some\\other/text";
var words3 = text3.split(/\////);

It just don't work. I don't know what I'm missing here.

1
  • Stackoverflow's syntax highlighting should be a hint for why your version is not working :) Commented May 16, 2015 at 2:01

1 Answer 1

3

The regex you want is

/[\/\\]/

The [] means "one of these characters", \/ represents a forward slash and \\ represents a backward slash. Since \ and / are special characters in regular expressions, I had to escape them by escaping them with a preceding backslash.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.