Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9042185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:25:24+00:00 2026-06-16T10:25:24+00:00

I’m trying to split words up so I can count the syllable in each

  • 0

I’m trying to split words up so I can count the syllable in each one, but i can’t seem to isolate the words at the moment, i can only count the whole number of syllables in the sentence,

        var text = $(this).val();
        var words = new Array(text.replace(/\s/g, ' ').split(' '));
        function countSyllables(words) {
            word = words.toString();
            var lWords = word.toLowerCase();
            var wordsArray=lWords.split(",");
            for (var z = 0; z < lWords.length; z++) {
                console.log(wordsArray, "- array of words");
                console.log(wordsArray[z], "- one item[z]");

My problem seems to be that i have to convert the array to a string to lower it, but then i can’t refer to each individual word, in both cases, to count the syllables:

[16:04:02.726] ["this", "is", "test", "text", "", "the", "words", "need", "to", "be", "separately", "analysed.", "hello"] - array of words    
[16:04:02.726] o - one item[z] 

This is the last character, when i want to refer to the word

I’m sure it’s a simple solution but i can’t wrap my head around it

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T10:25:25+00:00Added an answer on June 16, 2026 at 10:25 am

    This line is wrong on many levels:

    var words = new Array(text.replace(/\s/g, ' ').split(' '));
    

    What you’re after is, AFAIK, something like this:

    var words = text.split(/[^a-z0-9\-]+/gi);
    

    This splits the string, using all white-space, dots, brackets and other punctuation-things as delimiters. I’ve excluded the dash (-), to avoid counting words like ice-cream as two words. If you don’t care about that, or if you feel that the result is more reliable when you do treat dashes as delimiters, just leave them out:

    var words = text.split(/[^a-z0-9]+/gi);
    console.log(text + ' is approx. ' + words.length + ' words long');
    

    On the converting to lower-case thing: why not convert the entire text to lower-case from the off?

    var words = text.toLowerCase().split(/[^a-z0-9\-]+/gi);
    

    Oh, and the output of this code does not leave you with empty strings in the array:

    'Foo... bar, Foobar and lorum ipsorum'.toLowerCase().split(/[^a-z0-9\-]+/gi);
    //output: ["foo", "bar", "foobar", "and", "lorum", "ipsorum"]
    //length returns 6, which I think is correct
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I want to count how many characters a certain string has in PHP, but
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.