This question already has answers here:
How to find a number in a string using JavaScript?
(9 answers)
Closed 6 years ago.
I have a string that looks like:
var a = "value is 10 ";
How would I extract just the integer 10 and put it in another variable?
You could use a regex:
var val = +("value is 10".replace(/\D/g, ""));
\D matches everything that's not a digit.
you can use regexp
var a = "value is 10 ";
var num = a.match(/\d+/)[0] // "10"
console.log ( num ) ;
You can use some string matching to get an array of all found digits, then join them together to make the number as a string and just parse that string.
parseInt(a.match(/\d/g).join(''))
However, if you have a string like 'Your 2 value is 10' it will return 210.
You do it using regex like that
const pattern = /\d+/g;
const result = yourString.match(pattern);
Related
This question already has answers here:
Get the first integers in a string with JavaScript
(5 answers)
Closed 6 months ago.
How to find the first number from string in javascript?
var string = "120-250";
var string = "120,250";
var string = "120 | 250";
Here is an example that may help you understand.
Use the search() method to get the index of the first number in the string.
The search method takes a regular expression and returns the index of the first match in the string.
const str = 'one 2 three 4'
const index = str.search(/[0-9]/);
console.log(index); // 4
const firstNum = Number(str[index]);
console.log(firstNum); // 2
Basic regular expression start of string followed by numbers /^\d+/
const getStart = str => str.match(/^\d+/)?.[0];
console.log(getStart("123,456"));
console.log(getStart("123-456"));
console.log(getStart("123|456"));
console.log(getStart("xxx,xxx"));
Or parseInt can be used, but it will drop leading zeros.
const getStart = str => parseInt(str, 10);
console.log(getStart("123,456"));
console.log(getStart("123-456"));
console.log(getStart("123|456"));
console.log(getStart("xxx,xxx"));
This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 4 years ago.
I want to split a string by a variable number of successive characters
splitBy4('XXXXXXXX') => ['XXXX', 'XXXX']
Before injecting the variable it worked all fine :
console.log('XXXXXXXX'.split(/(\w{4})/).filter(Boolean));
// outputs : ['XXXX', 'XXXX']
console.log('XXXXXXXX'.split(new RegExp(/(\w{4})/)).filter(Boolean));
// outputs : ['XXXX', 'XXXX']
But when I try to use the RegExp class + string representation (to inject my parameter), it fails :
console.log('XXXXXXXX'.split(new RegExp('(\w{4})')).filter(Boolean));
// outputs ['XXXXXXXX']
const nb = 4;
console.log('XXXXXXXX'.split(new RegExp('(\w{'+ nb +'})')).filter(Boolean));
// outputs ['XXXXXXXX']
What am I missing and how can I inject my parameter ?
Thanks
const nb = "4";
var myRegex = new RegExp('(\\w{' + nb + '})', 'g');
var myArray = myRegex.exec('XXXXXXXX');
console.log(myArray.toString());
This question already has answers here:
How to increment number in string using Javascript or Jquery
(6 answers)
Closed 6 years ago.
I may have following type strings
A1 or 1A or AB....1 or 1AB......
so how to increment only digits of above type of strings in javascript?
var adminNo = data.Admission_No.slice(-2);
alert(adminNo);
var removedNo = data.Admission_No.substring(data.Admission_No.length-1);
alert(removedNo);
Use the replace method as shown in demo below
function incrementer(input)
{
return input.replace(/\d+/, function(match){ return parseInt(match) + 1 });
}
alert(incrementer("A1"));
alert(incrementer("1A"));
This will find the integer anywhere in the input string and increment it by one.
string.replace(/\d+/, function(n){ return ++n });
You can do it by taking out integer from your string
Its big long, but more self-explainatory
var youroriginalstring="A1"
var withNoDigits = youroriginalstring.replace(/[0-9]/g, '');
var yournumber = youroriginalstring.replace ( /[^\d.]/g, '' );
var incNos=yournumber +1;
var newString = incnos + "withNoDigits"
This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 8 years ago.
How can I take a string and split it at a special character into two new variables (and remove the special chars) with javascript?
For example take:
var X = Peggy Sue - Teacher
and turn it into:
varnew1 = Peggy Sue
varnew2 = Teacher
I guess it should also include a condition... if the string has a "-" then do this.
.split is probably what you want. Here is a very simple example
JSFiddle Link
var string = 'Peggy Sue - Teacher'
var new1 = string.split('-')[0].trim();
var new2 = string.split('-')[1].trim();
console.log(new1); // "Peggy Sue"
console.log(new2); // "Teacher"
And if you want to place a simple condition on it looking for - you can do so with the following
var string = 'Peggy Sue - Teacher'
var new1 = string.indexOf('-') !== -1 ? string.split('-')[0].trim() : string
var new2 = string.indexOf('-') !== -1 ? string.split('-')[1].trim() : string
Second Fiddle
var result = str.split("-");
will give you an array with 2 members,
result[0] = Peggy Sue
result[1] = Teacher
This question already has an answer here:
Get part of the string using regexp [closed]
(1 answer)
Closed 9 years ago.
Here's an example http://jsbin.com/USONirAn/1
var string = "some text username#Jake# some text username#John# some text some text username#Johny# userphoto#1.jpg#";
var type = "username";
var regexp = new RegExp(type + "#(.*?)#");
var matches = string.match(regexp);
Current regexp returns into matches an array with 3 items - [username#Jake#, username#John#, username#Johny#].
How do I make it return only a strings that I used to search for - (.*?)? In this example is should be an array [Jake, John, Johny]. Is it possible to get this only by changing a regexp function?
Update:
I've also tried to use exec function, but it returns both [username#Jake#, Jake] http://jsbin.com/USONirAn/6
Search-and-don't-replace
var matches = []
string.replace(regexp, function () {
matches.push(arguments[1]);
});
http://jsbin.com/USONirAn/4