codefossa Posted March 31, 2011 Share Posted March 31, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/232253-replace-regex/ Share on other sites More sharing options...
Adam Posted March 31, 2011 Share Posted March 31, 2011 A 'wildcard' is a character that can take any value. What does the ampersand indicate here? Looks like you're trying to parse out the text after it? Quote Link to comment https://forums.phpfreaks.com/topic/232253-replace-regex/#findComment-1194847 Share on other sites More sharing options...
salathe Posted March 31, 2011 Share Posted March 31, 2011 "sentence test&new".replace(/(.*)&/, "") Quote Link to comment https://forums.phpfreaks.com/topic/232253-replace-regex/#findComment-1194891 Share on other sites More sharing options...
.josh Posted March 31, 2011 Share Posted March 31, 2011 "sentence test&new".replace(/(.*)&/g, "") technically you need a g modifier on there. By default, preg_replace() replaces all. By default, .replace() does not. ...though based on context of the subject...it probably doesn't matter... Quote Link to comment https://forums.phpfreaks.com/topic/232253-replace-regex/#findComment-1194989 Share on other sites More sharing options...
salathe Posted March 31, 2011 Share Posted March 31, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/232253-replace-regex/#findComment-1195006 Share on other sites More sharing options...
codefossa Posted March 31, 2011 Author Share Posted March 31, 2011 Ohhhh, you don't put the regex inside of a string. Thanks very much! Quote Link to comment https://forums.phpfreaks.com/topic/232253-replace-regex/#findComment-1195225 Share on other sites More sharing options...
salathe Posted March 31, 2011 Share Posted March 31, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/232253-replace-regex/#findComment-1195286 Share on other sites More sharing options...
.josh Posted March 31, 2011 Share Posted March 31, 2011 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' Quote Link to comment https://forums.phpfreaks.com/topic/232253-replace-regex/#findComment-1195288 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.