JQuery: how to replace all between certain characters?

后端 未结 2 911
予麋鹿
予麋鹿 2020-12-06 23:32

I have searched for a general solution to this but only find answers to peoples specific questions.

Basically, I want to know how to generally use .replace() to repl

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-07 00:17

    What you are looking for are called regular expressions. For more information, you can visit a site like: http://www.regular-expressions.info/

    Note that regular expressions are not specific to JavaScript.

    For your specific example:

    string.replace(/abc.+xyz/,"abc"+newString+"xyz");
    

    . means any character, and + means one or more occurences.

    If you have more than one replacement to do, try:

    string.replace(/abc.+?xyz/g,"abc"+newString+"xyz");
    

    g stands for general, and ? is the lazy quantifier, meaning that it will stop at the next occurence of xyz in the string.

提交回复
热议问题