I have a javascript value holding a string:
value = "123 / 15 - Value1"
And I'm trying to split on delimiters so that I can have each number and the string value is their own variables.
I tried this:
value = "123 / 15 - Value1"
splitVal = value.split(/[" ".,\/ -]/);
number1 = splitVal[0];
number2 = splitVal[1];
name = splitVal[2];
But when I console log number1,number2 and name I only get '123' in the console, the other 2 are blank.
What am I doing wrong here as far as splitting a string by both a hyphen and a slash, as well as spaces?
9 Answers 9
You can use String.prototype.match() and a destructuring assignment with the following RegExp:
const value = '123 / 15 - Value1'
const [, number1, number2, name] = value.match(/(\d+) *\/ *(\d+) *- *(.+)/)
console.log(number1, number2, name)
Comments
The problem is that you split at (space) and / so 123 / 15 becomes:
[
'123',
// space
'',
// /
'',
// space
'15'
]
If you know how the pattern should look like you might want to use match instead of split:
value = "123 / 15 - Value1"
matchVal = value.match(/^(\d+)\s*\/\s*(\d+)\s*-\s*(.*)$/);
console.dir(matchVal)
With match you can exactly define how the input has to be formatted and which fields of the input you want to extract.
Furthermore it allows name to be something like Value-1 with spaces if this is required, (if this is not allowed you could change the (.*) a more restrictive match):
value = "123 / 15 - Value-1 with spaces"
matchVal = value.match(/^(\d+)\s*\/\s*(\d+)\s*-\s*(.*)$/);
console.dir(matchVal)
Using regex101.com an help you to understand what earch part of the regexp does.
4 Comments
10 10 / 10 Name then you might get an error (or an unwanted execution path) at an unexpected place in your code.You could use match() instead of split():
var value = "123 / 15 - Value1"
var splitVal= value.match(/[a-z0-9]+/gi);
console.log(splitVal)
1 Comment
With
console.table("123 / 15 - Value1".split(/[" ".,\/ -]/))
you'll find out what really happens is that all values except for indexes 0,3,6 are empty strings
Comments
You might have more luck just using a regex to pull out the values instead of trying to use delimiters.
let str = '123 / 15 - Value1';
let reg = /\w+/g;
let res;
let results = [];
while ((res = reg.exec(str)) !== null) {
results.push(res);
}
if (results.length > 0) {
console.log('Results = ' + results);
} else {
console.log('No results.')
}
Comments
var value = "123 / 15 - Value1";
var r = value.split(/\s?[\/-]\s?/);
console.log(r);
/*
[ '123', '15', 'Value1' ]
*/
Comments
The regex in your code is resulting in ["123", "", "", "15", "", "", "Value1"] .You can see in 1 & 2 index they are empty space. You can slightly modify the regex
var value = "123 / 15 - Value1";
var splitVal = value.split(/[\/-]/),
number1 = splitVal[0].trim(),
number2 = splitVal[1],
name = splitVal[2];
console.log(number1, number2, name)
Comments
This will work:
splitVal= value.split(/[" ".,\/ -]+/);
Add a '+' to the end of your regex. It tells the regex there must be at least one occurrence (i.e. no empty strings allowed) which is why your split is returning empty strings (as received from your regex).
Comments
You should remove white spaces before split, like this:
splitVal= value.replace(/ /g,'').split(/[" ".,\/ -]/);
5 Comments
"123 // 15 - Value1"?name before matching it.
valuein a generic way.[" "]I think you either want"or ` ` (space) here. This regex will match either of the two and uselessly repeats".[ /]the sequence between the two numbers123 / 15matches three times, hence why you get empty results. If you put a quantifier after the square brackers...]+you would only get three items after splitting.