user961954
user961954

Reputation: 3214

Split string into alphanumeric and others

How can I split a string into groups of alphanumeric and non-alphanumeric strings?

var str = "abcd !!hh8kK*( abcd efgh"
//some split logic that can give me
['abcd', ' !!', 'hh8kK', '*( ', 'abcd', ' ', 'efgh']

Upvotes: 0

Views: 906

Answers (3)

Rolice
Rolice

Reputation: 3103

This should work even with additional Unicode characters from different alphabets.

var result = "abcd !!hh8kK*( abcd efgh".split(/\b/g);

In the code above we split a string with regular expression on word boundaries \b - Word Boundaries.

Example Below:

var input = document.getElementById("input");
var output = document.getElementById("output");

var result = input.value.split(/\b/g);

result.forEach(function(item, index) {
  output.innerHTML += "Matched item at index " + index + ": <b>`" + item + "`</b><br> \n";
});
<input id="input" type="text" value="abcd !!hh8kK*( abcd efgh">
<p id="output"></p>

Upvotes: 2

jcubic
jcubic

Reputation: 66488

try:

var str = "abcd !!hh8kK*( abcd efgh";
var result = str.split(/(\w+)/);
result = result.slice(1, result.length-1); // remove trailing empty strings
document.body.innerHTML = JSON.stringify(result);

Upvotes: 3

James Donnelly
James Donnelly

Reputation: 128791

You can use the following regular expression:

/([A-Za-z0-9]+|[^A-Za-z0-9]+)/g

Regexper Visualisation

And apply it using JavaScript's match method:

"abcd !!hh8kK*( abcd efgh".match(/([A-Za-z0-9]+|[^A-Za-z0-9]+)/g)
-> ["abcd", " !!", "hh8kK", "*( ", "abcd", " ", "efgh"]

This matches one or more alphanumeric character or one of more non-alphanumeric character.

I've not used \w+ here as that matches both underscores (_) and hyphens (-).

Upvotes: 3

Related Questions