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

243
Views
Calculate the string length of only characters (not symbols) in the string using Javascript

I am trying to select words being passed through an IF statement based on only their character length, ignoring symbols.

For example If I have the word "hello!" I want the if statement to recognise it as a length of 5 characters.

I want to keep the symbol in the word when it is then added to updatedString. I just don't want to count the symbol in the original count. I have spent a while considering regex options but am struggling to find something that would work in this particular case.

An example of part of the code is below. (updatedString is declared outside of the if-loop)

 for (let word in arr) {
   if (arr[word].length == 5 || arr[word].length == 6) {
      let bold = arr[word].slice(0, 3)
      let normal = arr[word].slice(3)
      updatedString += `<b>${bold}</b>${normal} ` 
    }
   else if {
   ...
  }
 }

What I ultimately want is the area "arr[word].length == 5" to only count characters.

For the sake of this program I will assume that all words input in via the user will be those used in normal news or academic articles and nothing too obscure.

Any advice would be greatly appreciated, thanks.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use regex like so:

text = "hello!"
text.match(/[a-z]/g).join("") // join to return a string without any symbols

If you need to match more characters just update the pattern.

In your particular case it would look something like this:

for (let word in arr) {
    if (arr[word].match(/[a-z]/g).length == 5 || arr[word].match(/[a-z]/g).length == 6) {
        let bold = arr[word].slice(0, 3)
        let normal = arr[word].slice(3)
        updatedString += `<b>${bold}</b>${normal} ` 
    }else if (/* [...] */) {
        // [...]
    }
}
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!