0

Suppose we don't know how many slashes we could get in a string but we do not want any extra slashes. So if we get this string '/hello/world///////how/are/you//////////////' we should transform it to the form of '/hello/world/how/are/you/'. How to do it with the help of regular expressions in JavaScript?

asked Jun 1, 2010 at 13:58

4 Answers 4

3
"/hello/world///////how/are/you//////////////".replace(/\/+/g, "/")
answered Jun 1, 2010 at 14:00
Sign up to request clarification or add additional context in comments.

3 Comments

By the way, what does that g letter mean?
global replace, so basically it makes it a replaceAll which is what you want
Just expanding on the g: if it wasn't there, it would replace the first instance of / and then give up. The g flag effectively means, "keep on going until you've got them all."
1
'/hello/world///////how/are/you//////////////'.replace(/\/{2,}/g, '/');

This might be an incy wincy bit faster than mkoryak's suggestion, for it will only replace where necessary – i.e., where there's multiple instances of /. I'm sure someone with a better understanding of the nuts and bolts of the JavaScript regular expression engine can weigh in on this one.

UPDATE: I have now profiled mine and mkoryak's solutions using the above string but duplicated hundreds of times, and I can confirm that my solution consistently worked out several milliseconds faster.

answered Jun 1, 2010 at 14:10

1 Comment

Myself I tried doing the way you suggested ({2,}), but without the g letter. So it didn't work properly (replaced only once).
0

Edited: mkoryak's answer below is way better. Leaving this in case the info it contains is useful for someone else.

You could capture each word + slash group and look ahead (but don't capture) for one or more extra slash. Like...

(\w+\/)(?:\/)*(\w+\/)(?:\/)*

First () captures one or more of any word character followed by a slash, second () looks for a slash but doesn't capture, * means find 0 or more of the proceeding token. Etc.

Hope that helps!

answered Jun 1, 2010 at 14:05

Comments

0

I want to make a regex for string which matches from point A till point B

text= "testtttExecuted 'show bootvar' on \n10.238.196.66. kjdkhfkh Executed tsttt\n fhgkhkh"

Output should be

testtttExecuted 'show bootvar' on \n10.238.196.66. kjdkhfkh

I want to make a regex for string which matches from point A till point B
text= "testtttExecuted 'show bootvar' on \n10.238.196.66. kjdkhfkh Executed tsttt\n fhgkhkh"
Output should be
testttt<font color='red'>Executed 'show bootvar' on \n</font>10.238.196.66. kjdkhfkh <font color='red'>Executed tsttt\n</font> fhgkhkh

answered Oct 29, 2016 at 6:20

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.