如何更换" \ - "到" - "在网址?

时间:2014-07-30 15:31:19

标签: javascript jquery regex

我有文字:

car \- https://youtu.be/iOA7NUI\-9Xhw video\-one \-

我需要在网址中将\-替换为-,所以我得到:

car \- https://youtu.be/iOA7NUI-9Xhw vi\-deo \-

或者开始时可能会在每个单词中更改\-,这也会有很多帮助

car \- https://youtu.be/iOA7NUI-9Xhw vi-deo \-

3 个答案:

答案 0 :(得分:1)

/(^|[^ ])\\-($|[^ ])/g替换为$1-$2

$1指的是第一个捕获组($2相同)。

Regular expression visualization

Debuggex Demo

答案 1 :(得分:0)

只需使用replace方法,并使用以下正则表达式匹配仅包含在网址中的\-。我使用了URL中没有的空间逻辑。

\\-(?=\S)

DEMO

模式说明:

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

答案 2 :(得分:0)

使用replace javascript方法!

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