views:

98

answers:

8

Hi All,

I have below test cases as input:

  1. [This is my test] [This is my test] My name is xyz.

    Want Expected output as :
    [This is my test] My name is xyz.
    .

  2. [This is my test] My name is xyz.
    Want Expected output as:
    My name is xyz.

For above test cases I want to replace only first occurrence of '[This is my test] ' with blank. I don't want to replace second occurrence of match.

How do I resolve this using regex in JavaScript?

Thanks in advance.

ETA:

I just want to give more clarification that, i dont want to use hard coded value in regex , i want to use variable in regex.
Assume that [This is my test] is stored in one variable i.e. var defaultMsg = "[This is my test] ";

A: 

The JavaScript replace function defaults to non-global so it only replaces the first match:

var foo = '[This is my test] [This is my test] My name is xyz.';
var bar = foo.replace(/\[This is my test\]\s/, '');

If you wanted to replace all occurrences of the string then make the regex global by appending a g:

var bar = foo.replace(/\[This is my test\]\s/g, '');
LukeH
Thanks folks, I just want to give more clarification that, i dont want to use hard coded value in regex , i want to use variable in regex. Assume that [This is my test] is stored in one variable i.e. var defaultMsg = "[This is my test] ";
pravin
TRy Brock Adams approach - that one will work.
Thariama
A: 

Sure. Use replace():

var s = "[This is my test] [This is my test] My name is xyz.";
alert(s.replace(/\[This is my test\] /, ''));

If you want to replace all occurrences:

alert(s.replace(/\[This is my test\] /g, ''));
cletus
Thanks folks, I just want to give more clarification that, i dont want to use hard coded value in regex , i want to use variable in regex. Assume that [This is my test] is stored in one variable i.e.var defaultMsg = "[This is my test] ";
pravin
A: 

This will do what you want:

str= '[This is my test] [This is my test] My name is xyz.';

str = str.replace(/\[This is my test\]/,"");

To replace all occurences of '[This is my test]' you need to call:

str = str.replace(/\[This is my test\]/g,"");
Thariama
Thanks folks, I just want to give more clarification that, i dont want to use hard coded value in regex , i want to use variable in regex. Assume that [This is my test] is stored in one variable i.e. var defaultMsg = "[This is my test] ";
pravin
A: 

Anyone try this?

<script>
var defaultMsg ="[This is my test]"
var str         = "[This is my test] [This is my test] My name is xyz.";
str=str.replace(defaultMsg,"");
alert(str);
</script>

No need for regexp and replace does not care about special chars if the source string is not a regular expression object but just a string. Tested Mozilla 1.7, FF3.6.6, Safari 5, Opera 10 and IE8 windows XP sp3. Not sure I understand why this was voted down if it does the job with a minimum of fuss.

mplungjan
this won't work cause of the specialchars not beeing escaped
Thariama
Nope. Works. Try it
mplungjan
Yes, this works for THIS string on most browsers. But it's still improper. Also, what if you need to use `match()` `test()` or `exec()`? This approach fails.
Brock Adams
"improper" ? wow. downvoted for being improper posting code that works.
mplungjan
A: 
var str="[This is my test] [This is my test] My name is xyz.?";
var patt1=(/[[This is my test]].*My name is xyz/i);
document.write(str.match(patt1));
klox
+1  A: 

This will do the trick:

var foo = '[This is my test] ';// or whatever you want
foo = foo.replace(/([\[\]])/, '\\$1', 'g'); // add all special chars you want
var patern = new RegExp(foo);
var myString = '[This is my test] [This is my test] My name is xyz.';
var result = myString.replace(patern, '');
M42
A: 
var originalText = '[This is my test] [This is my test] My name is xyz.';
var defaultMsg = "[This is my test] ";

alert( originalText.replace( defaultMsg , '' ) );
Gaby
+1  A: 

If the search pattern is in a string variable and can contain special characters, then it must be escaped. Like so:

var defaultMsg  = "[This is my test] ";

//-- Must escape special characters to use in a RegEx.
defaultMsg      = defaultMsg.replace (/([\!\$\(\)\*\+\.\/\:\=\?\[\\\]\^\{\|\}])/g, "\\$1")

var zRegEx      = new RegExp (defaultMsg, '');

var Str         = '[This is my test] [This is my test] My name is xyz.';

Str             = Str.replace(zRegEx, "");

console.log (Str);  //-- Or use alert()
Brock Adams
Nope.defaultMsg='[This is my test]';'[This is my test] [This is my test] My name is xyz.'.replace(defaultMsg,""); works too.
mplungjan
@mplungjan: Did you test your approach on the top 5 browsers and for different combinations of special characters? Check it against the ECMAScript spec? What happens if the programmer needs to use `match()`, `test()` or `exec()` instead? The only way that works in all cases is to properly escape the string.
Brock Adams
But that is a different discussion. We are not talking match, test or exec, just replace the first occurrence of some string from another string. can be does with indexOf and length, but replace using a string works. Tested now you insist on Mozilla 1.7, FF3.6.6, Safari 5, Opera 10 and IE8 - granted it is on windows XP sp3, but I rest my case
mplungjan