-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-

despotbg
  • 740
  • 6
  • 12

3 Answers3

1

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

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

Regular expression visualization

Debuggex Demo

sp00m
  • 47,968
  • 31
  • 142
  • 252
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
Braj
  • 46,415
  • 5
  • 60
  • 76
  • what if the dash was at the end of the url/word? – OGHaza Jul 30 '14 at 16:24
  • @OGHaza add `$` in pattern `\\-(?=\S|$)`. [DEMO](http://regex101.com/r/aD6uX1/2) – Braj Jul 30 '14 at 17:25
  • I meant like this [demo](http://regex101.com/r/aD6uX1/3), but OPs requirements don't make it clear what is necessary. Clearly your original answer solves the problem for his given example. – OGHaza Jul 31 '14 at 08:17
  • @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. – Braj Jul 31 '14 at 08:36
0

Use replace javascript method!

    text = text.replace('\-', '-');
apollosoftware.org
  • 12,161
  • 4
  • 48
  • 69
  • 1
    don't you think that it will replace all `-` where is OP is looking for url only. – Braj Jul 30 '14 at 15:43