Jump to content

replace() regex


codefossa

Recommended Posts

I need help with regex, using a wildcard.

 

In PHP, it would be like this.

 

$str = 'sentence test&new';

$new = preg_replace('/(.*)&/', ''); // $new would now be equal to "new"

 

How would I do this same thing with str.replace() in javascript?

Link to comment
Share on other sites

technically you need a g modifier on there.  By default, preg_replace() replaces all.  By default, .replace() does not.

 

Technically, it doesn't matter and is not needed; at least, not for this particular use. 

 

The only thing that I would change would be the dot, if the string can span multiple lines. The PHP code mentioned would also need to be modified in that case.

Link to comment
Share on other sites

Ohhhh, you don't put the regex inside of a string.

 

Nope, JavaScript has a particular kind of object for regular expressions (see RegExp) and a "literal" syntax for creating new RegExp objects (e.g. /abc/g), as you've already seen.

 

Thanks very much!

 

You're welcome.  :)

Link to comment
Share on other sites

To expand...(translation: salathe beat me but dammit i made a long post so I'm posting anyway!)

 

Ultimately, in javascript a regex pattern is actually an object.  So you can use the pattern directly in the .replace() like that because it will ultimately evaluate to a regex object.  However, you can setup a regex pattern, putting it in a "string" by creating a RegExp object...though in the end you are still creating an object.  So for example, you could alternatively do:

 

var str = 'sentence test&new';

// 2 arguments: "pattern", "modifiers"
var pattern = new RegExp("(.*)&","g");

str = str.replace(pattern,'');

 

In this example, you are putting putting the pattern in quotes - as a string.  But again, ultimately this creates an object.  The main benefit of doing it this way over using the pattern directly as an object (no quotes, directly in .replace()) is if you want to make part of the pattern a variable. 

 

Example:

var subject = 'bargain';
var foo = 'bar';

// works : use a variable and the pattern is the value of the variable
var pattern = new RegExp(foo,"g");
str1 = subject.replace(pattern,'');
alert(str1); 
// output : 'gain'

// does not work : it looks for the literal string 'foo' 
str2 = subject.replace(/foo/g,'');
alert(str2);
// output : 'bargain'

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.