Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
107
rated 0 times [  110] [ 3]  / answers: 1 / hits: 66013  / 7 Years ago, thu, march 16, 2017, 12:00:00

I'd like to split strings like


'foofo21' 'bar432' 'foobar12345'

into


['foofo', '21'] ['bar', '432'] ['foobar', '12345']

Is there an easy and simple way to do this in JavaScript?


Note that the string part (for example, foofo can be in Korean instead of English).


More From » regex

 Answers
137

Check this sample code


var inputText = "'foofo21' 'bar432' 'foobar12345'";
function processText(inputText) {
var output = [];
var json = inputText.split(' '); // Split text by spaces into array

json.forEach(function (item) { // Loop through each array item
var out = item.replace(/'/g,''); // Remove all single quote ' from chunk
out = out.split(/(d+)/); // Now again split the chunk by Digits into array
out = out.filter(Boolean); // With Boolean we can filter out False Boolean Values like -> false, null, 0
output.push(out);
});

return output;
}

var inputText = "'foofo21' 'bar432' 'foobar12345'";

var outputArray = processText(inputText);

console.log(outputArray); Print outputArray on console

console.log(JSON.stringify(outputArray); Convert outputArray into JSON String and print on console


[#58524] Tuesday, March 14, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mercedez

Total Points: 525
Total Questions: 103
Total Answers: 102

Location: Trinidad and Tobago
Member since Fri, Mar 24, 2023
1 Year ago
;