Dennis Martinez
Dennis Martinez

Reputation: 6512

How can I regex a group of characters in javascript? (\/)

I have a string and I want to regex out only certain characters that are "together." How can I go by doing this? The characters I'm trying to check for are the backslash next to the forward slash.

http://jsfiddle.net/H4e7p/2/

Example:

Here is a string I'm regexing against:

Hello world! How \ar/e you? \/\/\/\/\/\/\\\\\\\\/\/

This is what it should look like:

Hello world! How \ar/e you? ******\\\\\\\**

This is what it actually looks like:

Hello world! How *ar*e you? ***********************

And here is the regex I'm currently using:

/[\\/]/g

What does the regex have to look like in order to accomplish checking for a group of characters? (\/)

Upvotes: 2

Views: 127

Answers (2)

t.niese
t.niese

Reputation: 40842

The meaning of /[\\\/]/g is either \ or /

But you want /(\\\/)/g

edit corrected escaping of /

Upvotes: 1

VisioN
VisioN

Reputation: 145408

You used wrong regular syntax: [xyz] is

A character set. Matches any one of the enclosed characters.

Try the following regex instead: /\\\//g.

DEMO: http://jsfiddle.net/H4e7p/3/

Upvotes: 4

Related Questions