0

I'm pretty new to regex but I'm trying to use a variable inside my match.

So I have a string that is "Total: $168" I'm trying to get the number amount, 168.

So I have this:

totalCost = totalCost.match(/[^Total: $]*$/);

when i echo that out I get 168. This is working, this is what I want.

But now I want to take it one step further and want to make "Total: $" a variable so I can easily set it and make this modular.

So I did

 var stringToSearch = 'Total: $';

and then did

 totalCost = totalCost.match(/[^stringToSearch]*$/);

I do a console log of:

 console.log(totalCost+" || "+stringToSearch );

and I get:

l: $168 || Total: $

Why when I make this variable it behaves all weird?

Jon Surrell
  • 9,444
  • 8
  • 48
  • 54
Travis
  • 157
  • 1
  • 13
  • 1
    Are you aware, that `[^stringToSearch]` means any character, that is not one of that's `stringToSearch` composed of (inside a [negated character class](http://www.regular-expressions.info/charclass.html#negated)). If you just want to match digits at the end use `\d+$` or `[0-9]+$` – Jonny 5 Feb 06 '15 at 23:15
  • Your question is hard to understand. What is the value of `totalCost` when you do `totalCost = totalCost.match(/[^Total: $]*$/);`? It would be nice to have a block of commented code explaining what you're doing... – Jon Surrell Feb 06 '15 at 23:25

3 Answers3

2

It's pure luck that your regex worked to return "120"!

[^Total: $]*$ tells the regex parser to match anything other than the characters between the brackets [..] ('T','o','t','a','l',' ', or '$'), as many times as possible up to the end of the line ($ not a literal '$' character in this case). So what did it match? The only characters that fell outside of those in the character class: '1','2','0'.

What you were trying to do was capture the matched number after the literal string 'Total: $':

var totalCost = 'Total: $168',
    matches = totalCost.match(/^Total: \$([\d\.]*)/),
    totalCostNum = matches ? parseFloat(matches[1]) : 0;

To make that variable, you'll need to first escape your variable so the text can be matched literally and then use new RegExp to build your regex:

var totalCost = 'Total: $168',
    stringToMatch = 'Total: $',
    stringToMatchEscaped = stringToMatch.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'),
    stringToMatchRegEx = new RegExp(stringToMatchEscaped + /([\d\.]*)/.source),
    matches = totalCost.match(stringToMatchRegEx),
    totalCostNum = matches ? parseFloat(matches[1]) : 0;
Ben Grimm
  • 4,316
  • 2
  • 15
  • 24
1

Looks like you can't interpolate into a JavaScript regex. /[^stringToSearch]*$/ would match any substring ending with characters other than those in the literal string "stringToSearch". If you want to be modular, you can use the RegExp constructor:

totalCost = totalCost.match(new RegExp("[^" + stringToSearch + "]*$"));
Community
  • 1
  • 1
fzzfzzfzz
  • 1,248
  • 1
  • 12
  • 22
  • Exactly what I was looking for. Wish I knew RegExp existed. I was wondering if there was a function to convert a string into a regex expression so I could pop that into the match but I couldn't find one. Thank you. – Travis Feb 06 '15 at 23:28
1

It sounds like you want to make your regex into a variable you can use on different inputs. Try something like this:

var regex = /^Total: \$(\d+)/;
regex.exec('Total: $168');
// [ 'Total: $168', '168', index: 0, input: 'Total: $168' ]
regex.exec('Total: $123');
// [ 'Total: $123', '123', index: 0, input: 'Total: $123' ]

There are also some problems with the logic of your regex, which I have changed in my example. It's not matching as you expect.

Jon Surrell
  • 9,444
  • 8
  • 48
  • 54