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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:03:08+00:00 2026-05-30T02:03:08+00:00

How can I change this regular expression to remove everything from a string except

  • 0

How can I change this regular expression to remove everything from a string except alphabets and a ‘(single quote)?

pattern = /\b(ma?c)?([a-z]+)/ig;
  1. this pattern removes unwanted spaces and capitalizes the first letter and turns the rest into lower case
  2. By alphabets I mean English letters a-z.
  • 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-30T02:03:09+00:00Added an answer on May 30, 2026 at 2:03 am

    To remove characters, you’d need to use something that actually does that, like the string replace function (which can accept a regular expression as the "from" parameter).

    Then you’re just dealing with a normal application of a character class, which in JavaScript (and most other regular expression variants) is described using [...], where ... is what should be in the class. You’d use the ^ at the beginning to invert the meaning of the class:

    In your case, it might be:

    str = str.replace(/[^A-Za-z']/g, "");
    

    …which will replace except the English characters A-Z (ABCDEFGHIJKLMNOPQRSTUVWXYZ), a-z (abcdefghijklmnopqrstuvwxyz), and the single quote with nothing (e.g., remove it).

    let str = "This is a test with the numbers 123 and a '.";
    
    console.log("before:", str);
    str = str.replace(/[^A-Za-z']/g, "");
    console.log("after: ", str);

    However, note that alphabetic characters not used in English will not be excepted, and there are a lot of those in the various languages used on the web (and even, perversely, in English, in "borrowed" words like "voilà" and "naïve").

    You’ve said you’re okay with just English A-Z, but for others coming to this: In environemnts supporting ES2018 and above’s Unicode property matching, you could handle anything considered "alphabetic" by Unicode instead of just A-Z by using the \p{Alpha} property. The \p means "matching this Unicode property" (as usual, the lowercase version \p means "matching" and the uppercase version \P means "not matching") and the {Alpha} means "alphabetic":

    str = str.replace(/[^\p{Alpha}']/gu, "");
    

    (Note that, again, \p{Alpha} means "alphabetic" but because it’s in a negated character class, we’re excluding alphabetic characters.)

    Note the u flag on that, to enable newer Unicode features. That handles the "voilà" and "naïve" examples too:

    let str = "This is a test with the numbers 123 and a ' and voilà and naïve.";
    
    console.log("before:", str);
    str = str.replace(/[^\p{Alpha}']/gu, "");
    console.log("after: ", str);
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I use regular expressions to change this string ':: 1:62 2:31 ::
How can I change the following regular expression to make empty values (a.k.a String.Empty)
You can change the connection string at run-time like this. You make the connection
from this code will show in original color how can i change it to
Can somebody help me construct this regular expression, please... Given the following strings... "April
Can someone help me with a regular expression that would help me do this.
Hi I'm looking a regular expression that will change this: [check out this URL!](http://www.reallycoolURL.com)
I can set data in JTable constructor, and then user can change this data
(r'^/(?P<the_param>[a-zA-z0-9_-]+)/$','myproject.myapp.views.myview'), How can I change this so that the_param accepts a URL(encoded) as a
According to this website , you can change to command key sequence used by

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.