Separate characters, special characters and numbers from a string in JavaScript
In this tutorial, We will see how to separate characters, special characters and numbers from a string in JavaScript. We can use regular expressions for this purpose.
Using match() method
We can separate characters, special characters, and numbers from a string by using the regular expression and match()
method.
const string = "abc123#$%def456"; // matching all alphabetic characters const characters = string.match(/[a-z]+/gi).join(""); // matching all numbers const numbers = string.match(/\d+/g).join(""); // matching all non-alphanumeric characters const specialCharacters = string.match(/[^a-z\d]+/gi).join(""); console.log(characters); console.log(numbers); console.log(specialCharacters);
First of all, I have defined the string
variable that contains some characters, special characters, and numbers that we want to separate.
After that, I have matched and extracted the characters, special characters, and numbers from the string using the match()
method along with regular expressions.
/[a-z]+/gi
: Matches one or more consecutive alphabetic characters (case-insensitive). Theg
andi
flags indicate a global search and case-insensitive search./\d+/g
: Matches one or more consecutive numeric characters. Theg
flag indicates a global search./[^a-z\d]+/gi
: Matches one or more consecutive non-alphanumeric characters. The^
inside square brackets negate the character set, matching any character that is not a letter or digit. Theg
andi
flags indicate a global search and case-insensitive search.
Then the join()
method is used to concatenate all the matching substrings together into a single string.
Output
abcdef 123456 #$%
Using split() method
We can also separate characters, special characters, and numbers from a string by using the regular expression and split()
method.
const string = "abc123#$%def456"; // matching all alphabetic characters const characters = string.split(/[\W\d]+/).join(""); // matching all numbers const numbers = string.split(/[^\d]+/).join(""); // matching all non-alphanumeric characters const specialCharacters = string.split(/[A-Za-z0-9]+/).join(""); console.log(characters); console.log(numbers); console.log(specialCharacters);
The regular expression /[\W\d]+/
matches any non-word characters (\W
) or digits (\d
) one or more times (+
).
The regular expression /[^\d]+/
matches any characters that are not digits one or more times.
The regular expression /[A-Za-z0-9]+/
matches any letters (uppercase and lowercase) or digits one or more times.
The split()
method is used to split the original string into an array of substrings based on the regular expression passed as an argument. Then, the join()
method is used to concatenate the substrings back into a string without the characters we want to remove.
Output:
abcdef 123456 #$%
Using replace() method
The replace()
function in JavaScript can be used to separate characters, special characters, and integers from a string. To replace every instance of each character type with a particular separator, we may use regular expressions and the global flag (/g
).
const string = "abc123#$%def456"; // matching all alphabetic characters const characters = string.replace(/[^a-zA-Z]/g, ''); // matching all numbers const numbers = string.replace(/\D/g, ''); // matching all non-alphanumeric characters const specialCharacters = string.replace(/[\w\s]/g, ''); console.log(characters); console.log(numbers); console.log(specialCharacters);
All non-letter characters are removed from the string using the replace()
function and the regular expression /[a-zA-Z]/g
. No matter in what case a character is in, this regular expression matches it. We saved the outcome in the character
variable.
After that, I have deleted any non-digit characters from the string using the replace()
function and the regular expression /D/g
, and then I saved the outcome in the numbers
variable. If a character is not a digit, this regular expression matches it.
Lastly, I used the replace()
function and the regular expression /[ws]/g
to delete any letters or spaces from the string. And stored the result in the specialCharacters
variable. This regular expression matches any word character or space character.
Output:
abcdef 123456 #$%
Leave a Reply