I am trying to replace specific letter which is present in my string but the problem there is multiple letter I want to replace so I have to write all the replace code manually
eg:-
string.replace(/<!-- ct:text -->/g, "")
.replace(/<!-- ct:hold -->/g, "")
.replace(/<!-- ct:ol -->/g, "")
.replace(/<!-- ct:asset -->/g, "")
.replace(/<!-- ct:ques -->/g, "")
.replace(/<!-- ct:data -->/g, "")
as here You can see this are some small value which I am replacing but there are large keywords which I want to remove
for eg:-
string.replace(/<!-- ct:text -->/g, "")
.replace(/<!-- ct:hold -->/g, "")
.replace(/<!-- ct:ol -->/g, "")
.replace(/<!-- ct:asset -->/g, "")
.replace(/<!-- ct:ques -->/g, "")
.replace(/<!-- ct:data -->/g, "")
.replace(/<!-- ct:query {\"queryId\":8,\"query\":{\"perPage\":\"10\",\"pages\":0,\"offset\":0,\"postType\"} -->/)
.replace(/<!-- ct:spacer {\"height\":120} -->/)
Is there any possible I can remove only the values which start with <!-- ct:
and if end with -->
and doesn't matter how long the replace text is
CodePudding user response:
string.replace(/^<\!-- ct:.*-->$/gi, "");
CodePudding user response:
You can use the regex wildcard match (.
) and repeat 1 or more times modifier (
) to match any characters within the comment after the ct: part. Also per @MonkeyZeus's comment, a lazy quantifier (?
) is needed so that it doesn't match from the start of the first comment all the way to the end of the last one in the string: /^<\!-- ct:. ? -->$/g
.