Is there an equivalent in jQuery to PHP's `preg_replace()`?

后端 未结 3 1680
无人及你
无人及你 2021-01-17 11:13

Say I have the following:


This path could be anything, we basically want to get the

3条回答
  •  日久生厌
    2021-01-17 11:36

    jQuery is not necessary here; Javascript supports regular expressions on its own, so jQuery is not part of the answer.

    Javascript's regex replace function is simply called .replace(), and is a method on the string class. You would use it as follows:

    var mystring = 'this is a string';
    mystring.replace(/is a/,'might be a');
    //mystring is now equal to 'this might be a string'.
    

    That should be enough to get you started. Since you referenced preg_replace() in the question, I assume you already know how to use regular expressions well enough not to need a detailed discussion of how to solve your specific example.

提交回复
热议问题