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 6560823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:30:40+00:00 2026-05-25T13:30:40+00:00

I am tired to remove all the words less than 3 characters, like in,

  • 0

I am tired to remove all the words less than 3 characters, like in, on ,the....

My code not work for me, Uncaught TypeError: Object ... has no method 'replace' ask for a help.

var str = 'Proin néc turpis eget dolor dictǔm lacínia. Nullam nǔnc magna, tincidunt eǔ porta in, faucibus sèd magna. Suspendisse laoreet ornare ullamcorper. Nulla in tortòr nibh. Pellentesque sèd est vitae odio vestibulum aliquet in nec leo.';
var newstr = str.split(" ").replace(/(\b(\w{1,3})\b(\s|$))/g,'');
alert(newstr);
  • 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-05-25T13:30:41+00:00Added an answer on May 25, 2026 at 1:30 pm

    You need to change the order of split and replace:

    var newstr = str.replace(/(\b(\w{1,3})\b(\s|$))/g,'').split(" ");
    

    Otherwise, you end up calling replace on an array, which does not have this method.

    See it in action.

    Note: Your current regex does not correctly handle the case where a “short” word is immediately followed by a punctuation character. You can change it slightly to do that:

    /(\b(\w{1,3})\b(\W|$))/g
                    ^^
    

    Apart from that, you also have to take care of the fact that the resulting array may contain empty strings (because deleting consecutive short words separated by spaces will end up leaving consecutive spaces in the string before it’s split). So you might also want to change how you split. All of this gives us:

    var newstr = str.replace(/(\b(\w{1,3})\b(\W|$))/g,'').split(/\s+/);
    

    See it in action.

    Update: As Ray Toal correctly points out in a comment, in JavaScript regexes \w does not match non-ASCII characters (e.g. characters with accents). This means that the above regexes will not work correctly (they will work correctly on certain other flavors of regex). Unfortunately, there is no convenient way around that and you will have to replace \w with a character group such as [a-zA-Zéǔí], and do the converse for \W.

    Update:

    Ugh, doing this in JavaScript regex is not easy. I came up with this regex:

    ([^ǔa-z\u00C0-\u017E]([ǔa-z\u00C0-\u017E]{1,3})(?=[^ǔa-z\u00C0-\u017E]|$))
    

    …which I still don’t like because I had to manually include the ǔ in there.

    See it in action.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What I like to do is remove all functions which has specific word for
I tried my best to remove all words in a sentence with numbers in
I would like to remove all substrings from a string in java that begin
I am trying to convert some code that is meant to remove all non-numeric
I need to remove all non-letter characters from the beginning and from the end
I'm trying to remove the 8px margin from the body tag. I've tried (not
I am trying to remove all the non-word character in a string, but want
I have a file like this: my line - some words & text oh
I have two branches that are different enough that rebasing seems to not work
I got a wordlist which is 56GB and I would like to remove doubles.

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.