i have following string , s1
, part of longer text (which doesn't have following patter in i.e. pattern happens once through out text. text includes white spaces , new lines)
<!-- components_list_start --> * [line-vis](line-chart) * [trend-vis](trend-vis) <!-- components_list_end -->
i want replace following string , s2
:
<!-- components_list_start --> * [line-vis](line-chart) * [trend-vis](trend-vis) * [common-vis](common-vis) <!-- components_list_end -->
i use following regex doesn't match :
str1.replace(/<!-- components_list_start -->(\s|.)*<!-- components_list_end -->/, str2)
doesn't : (\s|.)*
mean , characters including white space characters ?
you using greedy regex mess things. btw, can use [\s\s]
ungreedy instead this:
str1.replace(/<!-- components_list_start -->[\s\s]*?<!-- components_list_end -->/, str2)
the idea behind [\s\s]*?
match until first occurrence of pattern, in case <!-- components_list_end -->
and trott pointed in answer, assign string result:
str1 = str1.replace(/<!-- components_list_start -->[\s\s]*?<!-- components_list_end -->/, str2);