0

I have the following string , s1 , as a part of a longer text (which doesn't have the following patter in it i.e. this pattern only happens once through out the text. The text also includes white spaces and new lines)

<!-- COMPONENTS_LIST_START -->
* [line-vis](line-chart)
* [trend-vis](trend-vis)
<!-- COMPONENTS_LIST_END -->

I want to replace it with the following string , s2 :

<!-- COMPONENTS_LIST_START -->
* [line-vis](line-chart)
* [trend-vis](trend-vis)
* [common-vis](common-vis)
<!-- COMPONENTS_LIST_END -->

I use the following RegEx but it doesn't match it :

str1.replace(/<!-- COMPONENTS_LIST_START -->(\s|.)*<!-- COMPONENTS_LIST_END -->/, str2)

Doesn't this : (\s|.)* mean , all characters including white space characters ?

5
  • You are looking for the /.../s modifier Commented Apr 27, 2015 at 22:51
  • Could you please explain more ? Commented Apr 27, 2015 at 22:52
  • Also, you probably need the non-greedy quantifier (*?) if your string might have multiple components lists. Commented Apr 27, 2015 at 22:53
  • stackoverflow.com/questions/1068280/… Commented Apr 27, 2015 at 22:53
  • Just edited the question. Commented Apr 27, 2015 at 23:01

2 Answers 2

2

You are using a greedy regex so you will mess things. Btw, you can use [\S\s] ungreedy instead like this:

str1.replace(/<!-- COMPONENTS_LIST_START -->[\S\s]*?<!-- COMPONENTS_LIST_END -->/, str2)

The idea behind [\S\s]*? is to match everything until the first occurrence of your pattern, in your case <!-- COMPONENTS_LIST_END -->

And also as Trott pointed in his answer, assign the string a result:

str1 = str1.replace(/<!-- COMPONENTS_LIST_START -->[\S\s]*?<!-- COMPONENTS_LIST_END -->/, str2);
Sign up to request clarification or add additional context in comments.

1 Comment

"this pattern only happens once through out the text" so making it non-greedy will only improve performance, not make it "more correct". Also [\S\s] should be written as [^] (the negation of nothing).
2

Your regular expression works. The problem you are experiencing is that .replace() does not mutate the string. It returns a new string. If you want it to mutate the string, you need to assign the return value.

str1 = str1.replace(/<!-- COMPONENTS_LIST_START -->(\s|.)*<!-- COMPONENTS_LIST_END -->/, str2);

4 Comments

Actually, OP regex is greedy so technically isn't working, you are using mine :)
Oh shoot, I accidentally copy/pasted the regexp from your answer thinking I was copy/pasting from his question! Will edit...
No worries at all, just wanted to let you know that OP regex wasn't working
His works, but it replaces the entire string rather than just the inner part. But it appears that's what he wants/expects. Yours is better, though. Leaves the common prefix/suffix intact and all that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.