skip to Main Content

I have a regex to remove the html tags to count my characters. Which is working fine.

<string_variable>.replaceAll("<(.|n)*?>","").replaceAll("&nbsp;"," ").replaceAll("&amp;","&");

alert <string_variavle>.length()

But I need to add the
(New Line) to count my characters.

ABC

DEC

For example above ABC and DEC are 6 characters with 2 break lines it should be 9. As of now it is just counting with 6 characters.

Search for similar issue.
Tweaking my code

2

Answers


  1. You can systematically replace all of your n to a different char and then use the length:

    const temp = myVar.replaceAll("n", "a")
    console.log(temp.length)
    
    Login or Signup to reply.
  2. let str = `
        <div>
          abc
        </div>
        <div>
          abc
        </div>`
    
    console.log(countNewlines(str))
    
    function countNewlines(input) {
      let count = 0;
      for (let i = 0; i < input.length; i++) {
        if (input[i] === 'n') {
          count++;
        }
      }
      return count;
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search