• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Remove All Non-Alphanumeric Characters From a String

Using JavaScript to Remove All Non-Alphanumeric Characters From a String

July 25, 2022 Leave a Comment

In JavaScript, we can remove all non-alphanumeric characters from a string easily by making use of the JavaScript String replace() method and a regex expression.

var someString = "!This is $a )string_with Non-Alphanumeric characters@";

someString = someString.replace(/[^0-9a-z]/gi, '');

console.log(someString);

#Output:
ThisisastringwithNonAlphanumericcharacters

Notice in the replace method above, that instead of using .replace("/[^0-9a-z]/", '') we use replace(/[^0-9a-z]/gi, ''). If we used the expression "/[^0-9a-z]/" in the replace function, it would only replace the FIRST instance of a non-alphanumeric character. Using the regular expression /[^0-9a-z]/gi makes it so we replace ALL instances of a non-alphanumeric character in the string.


When using string variables in JavaScript, we can easily perform string manipulation to change the value of the string variables.

One such manipulation is to remove characters from a string variable. Non-alphanumeric characters can be troubling characters to deal with in string variables.

We can easily remove non-alphanumeric characters from a string in JavaScript.

The easiest way to get rid of a non-alphanumeric character in a string using JavaScript is with the JavaScript String replace() function.

The replace() function takes two arguments: the substring we want to replace, and the replacement substring. In this case, to remove non-alphanumeric characters, we pass the regex for non-alphanumeric characters (“[^0-9a-z]”) as the first argument and an empty string as the second argument.

Below is our example again of how you can remove non-alphanumeric characters from strings in JavaScript using the replace() function.

var someString = "!This is $a )string_with Non-Alphanumeric characters@";

someString = someString.replace(/[^0-9a-z]/gi, '');

console.log(someString);

#Output:
ThisisastringwithNonAlphanumericcharacters

Hopefully this article has been useful for you to learn how to use JavaScript to remove all non-alphanumeric characters from a string.

Other Articles You'll Also Like:

  • 1.  JavaScript Sorted Map – How to Sort a Map by Keys or Values
  • 2.  Using JavaScript to Remove Apostrophe From a String
  • 3.  How to Check if a Number is Even or Odd in JavaScript
  • 4.  JavaScript isInteger – How to Check if a Number is an Integer
  • 5.  Using JavaScript to Get the Height of an Element
  • 6.  Remove Word From String In JavaScript
  • 7.  How to Empty a String in JavaScript
  • 8.  Create Array of Zeros in JavaScript
  • 9.  How to Get the First Character of a String in JavaScript
  • 10.  JavaScript Capitalize First Letter of Every Word

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy

x