How to Split String at Specific Character in JavaScript

To split a string at a specific character in JavaScript, you can the “split()” function. The split() method takes a “separator” as its argument, which can be a string or a regular expression.

It divides the input string into an array of substrings, breaking the input string at each occurrence of the separator.

If the separator is not found in the input string, the method returns an array containing the entire input string as its single element.

Example

const inputString = 'nshah@gmail.com';
const separator = '.';

const resultArray = inputString.split(separator);

console.log(resultArray);

Output

['nshah@gmail', 'com']

In this example, we used the split() method to break the inputString into an array of substrings, using the separator variable (‘.’) as the delimiter. The resulting array contains two elements: ‘nshah@gmail‘ and ‘com’.

Leave a Comment