How to use jqueryjavascript replace as preg_replace in PHP? - Stack Overflow

admin2025-04-20  0

The preg_replace function in PHP provides a limit parameter. For instance, the first occurrence of medium replace with original, and the second occurrence of medium replace with "type".

$path = "demo\/medium\/Web081112_P001_medium.jpg";
$path = preg_replace ("/medium/","original",$path,1);
$path = preg_replace ("/medium/","type",$path,2);
echo $path;
// output : demo\/original\/Web081112_P001_type.jpg

So, are there any way using JQuery/ JavaScript to implement similar function? Thanks

The preg_replace function in PHP provides a limit parameter. For instance, the first occurrence of medium replace with original, and the second occurrence of medium replace with "type".

$path = "demo\/medium\/Web081112_P001_medium.jpg";
$path = preg_replace ("/medium/","original",$path,1);
$path = preg_replace ("/medium/","type",$path,2);
echo $path;
// output : demo\/original\/Web081112_P001_type.jpg

So, are there any way using JQuery/ JavaScript to implement similar function? Thanks

Share Improve this question edited Aug 17, 2017 at 10:44 masud_moni 1,27019 silver badges34 bronze badges asked Dec 10, 2012 at 2:08 user782104user782104 13.6k60 gold badges178 silver badges315 bronze badges 4
  • That 2 doesn't do what you think it does... the first occurrence has already been replaced by that time. – Ry- Commented Dec 10, 2012 at 2:21
  • 4 You don't use jQuery for string manipulation. Vanilla JS is a much better choice. – nnnnnn Commented Dec 10, 2012 at 2:30
  • @nnnnnn: OMG, haven't seen that link yet, that's hilarious! :D – Amadan Commented Dec 10, 2012 at 2:31
  • @Amadan - Yes, it's awesome. Did you notice you can select the features you want and download a custom JS include file? I think the site's only been around since the middle of this year - I'd love to know if somebody on SO is responsible; they should get a one-off platinum JS badge for it or something... – nnnnnn Commented Dec 10, 2012 at 2:51
Add a ment  | 

2 Answers 2

Reset to default 4

General solution:

var n = 0;
"a a a a a".replace(/a/g, function(match) {
  n++;
  if (n == 2) return "c";
  if (n == 3 || n == 4) return "b";
  return match;
})
// => "a c b b a"

Also, you are mistaken: the second replacement will not happen. Limit 2 doesn't say "second occurence", it says "two occurences"; however, after the first preg_replace you don't have two mediums any more, so the second preg_replace ends up replacing the single one that is left, and then quits in frustration. You could do the same using two single-shot (non-global) replace (or preg_replace) invocations, without using any limit.

are there any way using jquery/ javascript to implement similar function?

Use JavaScript's native replace().

By default, replace() will only replace the first occurrence - unless you set the g modifier.

Example:

path = "demo/medium/Web081112_P001_medium.jpg";
path = path.replace("/medium/", "original");
path = path.replace("medium", "type");

Note: Your code doesn't match your specification. I addressed your specification.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745149082a287516.html

最新回复(0)