-1

I have text:

https://youtu.be/iOA7NUI\-9Xhwvideo\-one\-

I need to replace \- to - with JS, so i get:

https://youtu.be/iOA7NUI-9Xhwvideo-one-

1
  • For now this is best solution return text.replace(/(\S)(\\)(-)(\S)/g, "$1$3$4"); Commented Jul 31, 2014 at 13:32

3 Answers 3

1

Replace /(^|[^ ])\\-($|[^ ])/g by $1-$2.

$1 refers to the first capturing group (same for $2).

Regular expression visualization

Debuggex Demo

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

Comments

0

Use replace javascript method!

    text = text.replace('\-', '-');

1 Comment

don't you think that it will replace all - where is OP is looking for url only.
0

Simply use replace method and use below regex to match \- only that contains in URL. I used the logic of space that is not there in URL.

\\-(?=\S)

DEMO

Pattern explanation:

  \\                       '\'
  -                        '-'
  (?=                      look ahead to see if there is:
    \S                       non-whitespace (all but \n, \r, \t, \f, and " ")
  )                        end of look-ahead

4 Comments

what if the dash was at the end of the url/word?
@OGHaza add $ in pattern \\-(?=\S|$). DEMO
I meant like this demo, but OPs requirements don't make it clear what is necessary. Clearly your original answer solves the problem for his given example.
@OGHaza thanks for your time and testing but there are lots of permutation and combination. I need more info about the rules. Need to verify with OP.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.