Reputation: 547
In this example, I want to extract the given price in the string.
const string = 'I am a long sentence! My price is $5,000 on a discount!';
const price = string.substring(string.lastIndexOf("$")+1,string.lastIndexOf(" "));
document.write(price);
The above method won't work because it selects the last index of a space (" "
)
How do I go about getting the space right after the price ?
EDIT:
The string might be anything else! I have no control over it, I just want to extract the price. In case I didn't make that clear.
Upvotes: 0
Views: 673
Reputation: 5020
const string = 'I am a long sentence! My price is $5,000 on a discount!';
const price =string.split('$')[1]//text after $
.split(' ')[0];// text after $ and before space
document.write(price);
const string = 'I am a long sentence! My price is $5,000 on a discount!';
const price =string.split('$')[1].split(' ')[0];
document.write(price);
or
const string = 'I am a long sentence! My price is $5,000 on a discount!';
const price =string.match(/\$[0-9]*,[0-9]*/);
//match $XXX,XXX in the string and estract it
document.write(price);
Upvotes: 2
Reputation: 21502
const string = 'I am a long sentence! My price is $5,000 on a discount!' +
'Another price: $120.';
var m = string.match(/\$[\d,]+/g);
var i, price;
if (m) {
for (i = 0; i < m.length; i++) {
price = m[i].replace('$', '');
console.log(price);
}
}
Upvotes: 1
Reputation: 41263
Regular expressions aren't always the right tool, but for this case it seems like an obvious choice. What you really want is everything that matches a $
followed by a mixture of numbers and commas, ending at any other character.
const re = /\$[\d,]+/
const string = 'I am a long sentence! My price is $5,000 on a discount!';
const prices = re.exec(string);
console.log(prices[0]);
You might want to expand the pattern, for example, to also match .
-- /\$[\d,.]+
(would capture "$5,000.25") or to be extra permissive, just anything but a space: /\$[^ ]+/
(would capture "$tonsofmoney").
Upvotes: 2
Reputation: 201
You can try:
const string = 'I am a long sentence! My price is $5,000 on a discount!';
const price = (string.split("$")[1].split(" ")[0]);
document.write(price);
Upvotes: 2
Reputation: 1551
Please try this. I'm splitting it into multiple rows for better readability.
const string = 'I am a long sentence! My price is $5,000 on a discount!';
var dollarPosition = string.lastIndexOf("$");
var nextSpace = string.indexOf(" ", dollarPosition+1);
const price = string.substring(dollarPosition+1,nextSpace);
document.write(price);
Upvotes: 1
Reputation: 177
you can use string.split()
const string = 'I am a long sentence! My price is $5,000 on a discount!';
const price = (string.split('$')[1]).split(' ')[0];
document.write(price);
this splits the string at the first index of $ and takes the part after the $.
than it splits the string at the first appearance of a space and takes the part before the space.
note that this expects that there is only 1 $ sign in the string.
Upvotes: 0