0

I'm trying to add a character to the beginning of certain strings with Regular Expressions but am very new to it and can't seem to find an answer for what I'm looking for. I have strings like Below 1499 and Above 1900 and I want to add a $ to the beginning of the number strings. Here's what I've got to locate the code (btw, these are all text characters in a div with a class of refinement_price_text):

$('.refinement_price_text').each(function(){
    console.log($(this).text().match(/\d{1,5}/g));
});

It logs them out to the console fine. They are logged as arrays with one item. I don't know how to prepend a dollar sign to them now though. I've tried prepend() and that doesn't work. I've tried to set the match() as a variable but that didn't work. I wanted to originally use replace() but I need to maintain the current values there and just add the dollar sign character to the beginning and I didn't know what the equivalent of $(this) is for a regular expression in order to keep the same values.

Let me know if this is making sense. I'm sure there must be a function that will easily do this? Thanks for your help!

0

2 Answers 2

2

I believe this handles all possibilities:

"111 Above 1499 and below 14930 and $100".replace(/([^$]|^)(\b\d+)/g, "$1$$$2")
> "$111 Above $1499 and below $14930 and $100"

To replace the text in Jquery:

$(this).text(function(i, t) { return t.replace(...above stuff...) })

http://jsfiddle.net/k7XJw/1/

To ignore numbers is parentheses,

str = "111 Above 1499 and below 14930(55) and $100 and (1234) and (here 123) and (123 there)"
str.replace(/([^$(]|^)(\b\d+\b)(?!\))/g, "$1$$$2")
> "$111 Above $1499 and below $14930(55) and $100 and (1234) and (here 123) and (123 there)"
Sign up to request clarification or add additional context in comments.

4 Comments

There are multiple numbers in some of the string. For example some will say 'Above 1499 (49)' with the number in the parentheses being the quantity. How would I ignore the numbers in the parantheses and just apply it to the numbers in the string. I know this is an add-on, I just realized it's an issue though...thanks for your help!
Is there always only one price value in a string? If yes, just drop the g flag.
More than one in some strings.
Perfect thank you. I'm going to study your code now so I can learn.
0

For simple strings like the one you posted a lookahead regex and replace will work. Basically telling the regex to find the first number in a string (but dont consume it) and then prepend a dollar sign. For multiple numbers in the same string you will have to tweak the regex.

var s = "before 1900"
s=s.replace(/(?=[0-9])/,"$");
console.log(s);

Modified to support multiple occurences. It looks for any number preceeded by a whitespace and then prepends the dollar sign to that number.

Plunker example

var s = "before 1900 and 2130 and (1900)"
s=s.replace(/\s(?=\d)/g," $");
console.log(s);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.