1

I have string it contain unwanted html tags and text, so I want to remove unwanted matching text and get my required values:

Code:

var mystring = "<!-- html-text: 143 --> value 1  <!-- /html-text --><!-- html-text: 144 --> | <!-- /html-text --><!-- react-text: 145 --> value 3 <!-- /html-text --><!-- html-text: 146 -->, <!-- /html-text --><!-- html-text: 147 --> value 2 <!-- /html-text --><!-- html-text: 148 --> <!-- /html-text --><!-- html-text: 149 -->value 4 <!-- /html-text -->";
mystring = mystring.replace('<!-- html-text: 143 -->','');

console.log('str'+mystring);

Required output:

value 1  value 2 value 3 value 4
10
  • @Bravo I need all value1,value2, value3,value4 separated by space.... in question I forgot to mentioned value 4 Commented Feb 28, 2022 at 5:40
  • browser code... using node js for scraping Commented Feb 28, 2022 at 5:44
  • @Bravo can I add , | in same regex to avoid those things Commented Feb 28, 2022 at 5:45
  • @Bravo thank you so much ,, thanks a lot You saved my lot of time... can u please explain this regex in 1 or 2 line if possible so that next time I can do it without any issue. Commented Feb 28, 2022 at 5:49
  • 1
    blog.bitsrc.io/… Commented Feb 28, 2022 at 5:49

1 Answer 1

1

You can do this with regex:

var mystring = "<!-- html-text: 143 --> value 1  <!-- /html-text --><!-- html-text: 144 --> | <!-- /html-text --><!-- react-text: 145 --> value 3 <!-- /html-text --><!-- html-text: 146 -->, <!-- /html-text --><!-- html-text: 147 --> value 2 <!-- /html-text --><!-- html-text: 148 --> <!-- /html-text --><!-- html-text: 149 -->value 4 <!-- /html-text -->";
mystring = mystring.replace(/<!--.*?-->/g,'');

console.log(mystring);

Regex explanation:

  1. <!--...--> Marks the opening and ending of an HTML comment
  2. .* Matches any character zero or more times
  3. ? Remove "greedy matching" (match the least possible instead of most)
  4. g Global, meaning to replace all occurrences instead of just one
Sign up to request clarification or add additional context in comments.

Comments

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.