0

EDIT: This question is DIFFERENT. The regex works fine that everyone sent me. The problem is that the $ sign DOESN'T WORK on my string, but it works on others. I can't figure out why and I posted my function below.

I need to find a number at the end of a string, it will be like thisL

My Goal Amount: $25.00/$100.00.

I've tried

var matches = fileDataResult.match(/\d+$/);

but it returns null, I tried it without the $ and it returns the 25, without the .00.

How can I get a variable as a number for the first ones (25.00, with the decimals) and a different variable for the 100.00, with the decimals as well. I need the variables seperate so I can work on them, but I think regex is broken because it won't even work with the $ sign... anyone have suggestion? This is in javascript.

edit: here is my function, it reads a .txt file and gets the string. I can console.log the string just fine and it work, but it won't work when I use $ in regex!

function fileReaderFunc(file) {
    const fileReader = new FileReader();
    fileReader.onload = function() {
        let fileDataResult = '';
        const fileData = fileReader.result;
        fileDataResult = fileData.toString();

        console.log(fileDataResult);

        let str = fileDataResult;
        let reg = /\d+(\.\d+)?$/g;
        console.log(str.match(reg));
    };
    fileReader.readAsText(file);
}
pushkin
  • 9,575
  • 15
  • 51
  • 95
supafiya
  • 137
  • 2
  • 9
  • `\d` does not match `.`. – SLaks Dec 14 '18 at 18:38
  • 1
    use this `\d+(\.\d+)?$` – Code Maniac Dec 14 '18 at 18:39
  • It seems `let reg = /\d+(?:\.\d+)?/g;` will work. – Wiktor Stribiżew Dec 14 '18 at 19:10
  • 2
    Your string ends with a dot, that's not included in your regex and will break, when you use '$', beacuse the number is not at the end of string. – Poul Bak Dec 14 '18 at 20:04
  • I'm voting to reopen because the duplicates do not properly address the problem: `$` is being improperly used. The `$` matches a line ending, which is not in either of the desired substrings in the question: `25.00` and `100.00`. A proper answer (or duplicate) would be one that indicates when a dollar sign is inappropriate. slevy1's answer touches on this, but neither duplicate does. – Ruzihm Dec 14 '18 at 20:33

2 Answers2

0

First of all \d+ do not match .

You can use this

\d+(\.\d+)?$

Explanation

  • \d+ - Matches one or more digits.
  • (\.\d+)? Matches . followed by one or more digit. (? makes it optional )

let str = "abc 125.00";
let reg = /\d+(\.\d+)?$/g;
console.log(str.match(reg))
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
  • I'm still getting null, works fine with your code but it wont work on my string. Here's my function, it reads text from file I can console log the string just fine, but when I use $ in regex it just doesn't work! `function fileReaderFunc(file) { const fileReader = new FileReader(); fileReader.onload = function() { let fileDataResult = ''; const fileData = fileReader.result; fileDataResult = fileData.toString(); console.log(fileDataResult); let str = fileDataResult; let reg = /\d+(\.\d+)?$/g; console.log(str.match(reg)); }; fileReader.readAsText(file); }` – supafiya Dec 14 '18 at 19:08
  • @supafiya does you string have numbers at any other place in string except at the end ? will it always be two numbers at the end ? – Code Maniac Dec 14 '18 at 19:21
  • yes it will always be numbers at the end. It just doesn't work with my string I don't know why. It works with regex until I use the $ sign, then it starts to return null – supafiya Dec 14 '18 at 19:23
0

let str = "My Goal Amount: $25.00/$100.00.";
str = str.substring(-1,str.length-1);  // remove the terminal period
let reg = /\$.+\$.+$/g;
let res = str.match(reg);
let arr = res[0].split('/');
let [num1,num2] = arr;                


console.log("1st: ", num1.substring(1, num1.length));
console.log("2nd: ", num2.substring(1, num2.length));

As it turns out this regex is simpler than one might suppose since the text itself includes dollar signs. One can construct a regex that searches for a literal dollar sign followed by other characters and then a second literal dollar sign also followed by other characters from the end of the string marked by a dollar sign symbol minus any slash which signifies the end of a string.

The match is stored in a result array containing one element whose string value is then split on the literal slash mark. The results are then stored in an array arr. Then the values stored in the array are assigned to variables num1 and num2 through array destructuring.

If you prefer a more focused regex, you can also code as follows:

let s = "My Goal Amount: $25.00/$100.00.";
s = s.substring(-1, s.length - 1);
let reg = /\$\d+\.\d+.\$\d+\.\d+$/;
let replaced = s.match(reg)[0].replace(/\$/g, "");
console.log(replaced.split("/"));

If you neglect to trim the string s of the terminal period, then the regex will not match with the string which results in a null. This regex specifies a pattern at the end of the string that starts with a dollar sign,followed by one or more digits then a period and one or more digits. The pattern continues with matching another character (in this case the slash mark) and then a dollar sign followed by one or more digits, next a period and again one or more digits.

slevy1
  • 3,797
  • 2
  • 27
  • 33
  • This is good, but it won't work for my string!! I don't get it, I edited my answer to show my function. I tried your regex too and I still get null.. – supafiya Dec 14 '18 at 19:13
  • @supafiya The issue may be your function rather than this being merely an issue about a regex and that is a different question. You asked for a working regex and I provided one. Check out this: https://developer.mozilla.org/en-US/docs/Web/API/FileReader/FileReader – slevy1 Dec 14 '18 at 20:10
  • 1
    This is the closest to the answer I'm looking for. Thanks I'm going to look into File reader a bit more. – supafiya Dec 14 '18 at 20:47