Extract number from string in javascript [duplicate] - javascript

This question already has answers here:
How to find a number in a string using JavaScript?
(9 answers)
How can I extract a number from a string in JavaScript?
(27 answers)
Closed 4 years ago.
I have string in the bellow format:
let str = "url(#123456)";
My string have only number contain in it. It can be any where.
I want to extract the number 123456 from the above string.
I am using es6.

str.replace(/[^0-9]/g, '')
let str = "url(#123456)";
console.log(str.replace(/[^0-9]/g, ''))

another way to do it
let str = "url(#123456)";
console.log(str.match(/\d+/)[0])

I did it in a way too complicated way, but it does not involve regex so it's probably better to understand:
let str = "url(#123456)";
str = str.split("#");
str = str[1].split(")");
str = str[0];
console.log(str);

Using only Array methods:
console.log("url(#123456)".split('#').pop().slice(0, this.length -1))

Related

Is there any function which can transform string into expression? [duplicate]

This question already has answers here:
What are the Alternatives to eval in JavaScript?
(11 answers)
Closed 8 months ago.
console.log(Math.sqrt(4));
let str = 'Math.sqrt(4)';
console.log(str);
Is there any way for the 2nd console.log to show 4?
Is there any function which can do this?
You can use eval(), like
let str = eval('Math.sqrt(4)');
To do this, you can use the eval() function to evaluate the code in the string. Like this:
let str = 'Math.sqrt(4)';
console.log(eval(str));

javascript - what does ${} do? [duplicate]

This question already has answers here:
What does ${} (dollar sign and curly braces) mean in a string in JavaScript?
(6 answers)
What does this symbol mean in JavaScript?
(1 answer)
Closed 2 years ago.
I have seen ${ } being used in many javascript codes. What exactly does this do?
For example:
updateDisplay(){
if(this.operation != null){
this.previousOperandTextElement.innerText = ${this.previousOperand} ${this.operation};
}
this.currentOperandTextElement.innerText = this.currentOperand;
}
Why would we not use + to concatenate in this case?
It's called a template literal. It achieves the same thing as concatenation but in a more readable manner:
const a = "Hello"
const b = "."
console.log(`${a} World${b}`)

Sorting strings in javascript [duplicate]

This question already has answers here:
How do you reverse a string in-place in JavaScript?
(57 answers)
Closed 3 years ago.
I would like to sort strings in javascript containing comma separated values in different e.g.
var Str = "8,0,2,10"
I want to sort it like below example form the last one to first one:
var NewStr = "10,2,0,8"
You can convert string to array using split() and reverse the array element using reverse() and then convert result to string again using join() like this:
var Str = '8,0,2,10';
var dif = Str.split(',').reverse().join(',');
console.log(dif);

convert string to an array using JQuery [duplicate]

This question already has answers here:
Convert string with commas to array
(18 answers)
Closed 4 years ago.
var string = "Chemistry,English,History";
Sir i want to convert it to an array like:
var subjects= ["Chemistry","English","History"];
using jQuery
No need of jquery. Just use .split() function to achieve this.
let string = 'Chemistry,English,History';
let arr = string.split(',');
console.log(arr)

get number only in javascript [duplicate]

This question already has answers here:
How can I extract a number from a string in JavaScript?
(27 answers)
Closed 9 years ago.
I have a string likes
AA-12,AB-1,AC-11,AD-8,AE-30
I want to get number only from this string likes
12,1,11,8,30
How can I get this using JavaScript ? Thanks :)
Use a regex, eg
var numbers = yourString.match(/\d+/g);
numbers will then be an array of the numeric strings in your string, eg
["12", "1", "11", "8", "30"]
Also if you want a string as the result
'AA-12,AB-1,AC-11,AD-8,AE-30'.replace(/[^0-9,]/g, '')
var t = "AA-12,AB-1,AC-11,AD-8,AE-30";
alert(t.match(/\d+/g).join(','));
Working example: http://jsfiddle.net/tZQ9w/2/
if this is exactly what your input looks like, I'd split the string and make an array with just the numbers:
var str = "AA-12,AB-1,AC-11,AD-8,AE-30";
var nums = str.split(',').map(function (el) {
return parseInt(el.split('-')[1], 10);
});
The split splits the string by a delimiter, in this case a comma. The returned value is an array, which we'll map into the array we want. Inside, we'll split on the hyphen, then make sure it's a number.
Output:
nums === [12,1,11,8,30];
I have done absolutely no sanity checks, so you might want to check it against a regex:
/^(\w+-\d+,)\w+-\d+$/.test(str) === true
You can follow this same pattern in any similar parsing problem.

Categories