This question already has answers here:
Javascript and regex: split string and keep the separator
(11 answers)
Splitting a string into chunks by numeric or alpha character with JavaScript
(3 answers)
Closed 6 years ago.
I have a string
var houseNumber = '13a';
I want to split the addition from the number so I can keep it in a other field.
How can I split this value without losing the number or the addition? At the end I would like to have 2 fields with the following type:
var houseNumber = 13; //Number
var addition = 'a'; //String
There are many questions about this, but I can't find one where both values has to be saved. That's why I created a new question.
Use the following code
var string_a = "jkjkhj89898";
var x = string_a.match(/[^\d]+|\d+/g);
console.log(x)
working fiddle.
Thanks
Related
This question already has answers here:
How can I split a string into segments of n characters?
(17 answers)
How do you use a variable in a regular expression?
(27 answers)
Closed 2 years ago.
I'm struggling with a very simple problem.
lines = "hogefoobarwai"
I want to cut this string into 4 characters.
Like this.
hoge, foob, arwa, i
How to split?
I try to use split() with regex.
let vars = lines.match(/.{4}/g);
This is good. But if something like {4} is variable, it won't work.
for example
length = 6
let vars = lines.match(/.{length}/g);
this shows literally /.{length}/.
If anyone can tell me what it is, please let me know.
You could take a minimum length of one (for getting smaller substrings) and the length and build a new regular expression.
const
lines = "hogefoobarwai",
length = 4,
parts = lines.match(new RegExp(`.{1,${length}}`, 'g'));
console.log(parts);
try using a dynamically generated Regex:
const newRegEx = new RegEx('{' + length + '}', g)
This question already has answers here:
How to split comma separated string using JavaScript? [duplicate]
(4 answers)
Closed 2 years ago.
I have a string like this
var assignments= "NAME1,NAME2,NAME3,NAME4,NAME5,NAME6"
how do i print only first three strings like this "NAME1,NAME2,NAME3" in JavaScript
You can do this way
let string= "NAME1,NAME2,NAME3,NAME4,NAME5,NAME6".split(',')
let output = string.splice(',',3).join(',')
console.log(output)
Using standard Array functions you can do what you want. The code below shows the various stages, though you can write it more concisely as one line if you like.
var assignments= "NAME1,NAME2,NAME3,NAME4,NAME5,NAME6"
var splitAssignements = assignments.split(",")
var firstThreeArray = splitAssignements.slice(0,3)
var firstThreeString = firstThreeArray.join(",")
console.log(firstThreeString)
This question already has answers here:
Split string into array [duplicate]
(10 answers)
Closed 5 years ago.
So I have a string similar to the following:
var mystring = "aasfeeeffdbals";
and I need to make it an array filled with the individual chars. I know what I can do that with the following method:
var charArray = []
for (var i = 0; i<mystring.length; i++) {
charArray.push(mystring[i]);
}
QUESTION: My understanding of a string is that it already in essence is an array of characters put together, so might there be a more straight forward way of creating a new array with the characters from the string?
var chars = "aasfeeeffdbals".split(''); will bring you wanted result
This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 8 years ago.
I have a string that always contains 8 variable values that are separated with a hypen (-) like in the following example:
5-2-2-2-2-2-2-1
What is the best way to split this into 8 separate values so that I can use each of them in a variable if the values can be either an integer or the value 'n/a' ?
Many thanks for any help with this, Tim.
var str = '5-2-2-2-2-2-2-1';
var parts = str.split('-');
for (var i=0;i<parts.length;i++){
console.log(parts[i]);
}
You are searching for the String.split() method
This question already has answers here:
How to match multiple occurrences of a substring
(3 answers)
Closed 2 years ago.
Let's say I have an input field and want to parse all of the numbers from the submitted string. For example, it could be:
Hi I'm 12 years old.
How do I parse all of the numbers without having a common pattern to work with?
I tried:
x.match(/\d+/)
but it only grabs the 12 and won't go past the next space, which is problematic if the user inputs more numbers with spaces in-between them.
Add the g flag to return all matches in an array:
var matches = x.match(/\d+/g)
However, this may not catch numbers with seperators, like 1,000 or 0.123
You may want to update your regex to:
x.match(/[0-9 , \.]+/g)
var words = sentence.split(" ");
var numbers = words.filter(function(w) {
return w.match(/\d+/);
})