Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
javascript, substring should exclude html tags in its character count

The substr should not consider html tags in its count, is there any way to achieve it?

for example, if I have

 input =  <p> This is testing <strong> hi </strong> how are you, Well doing  </p>

The input.substring(0, 20) should not consider <p> and <strong> tags in its character count.

The html tags should not be removed from the string, just they have to be ignore in the count for substring.

     <h2 v-html="this.$options.filters.limitText(input)" ></h2>

     filters: {
      limitText: function (val) {
       if(val && (val.length > 20)) {
         return (val.substring(0, 20).replace(/\s\S*$/, '') + ' ...')
       }
      return val;
    
      }
    }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Does this help:

const input = "<p> This is testing <strong> hi </strong> how are you, Well doing  </p>";
const tags = /<[^>]*>/g;
const output = input.replace(tags, '').substring(0, 20);
console.log(output); //-> ' This is testing  hi'
about 4 years ago · Juan Pablo Isaza Report

0

My suggestion:

const input = "<p> This is testing <strong> hi </strong> how are you, Well doing  </p>";
const count = input.replace(/(<([^>]+)>)/gi, "").length;

console.log(count); // Output: 47
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!