简体   繁体   English

javascript中的正则表达式,试图匹配但似乎不适用于$

[英]Regex in javascript, trying to match but doesn't seem to work with $

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. 我的目标金额:$ 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. 但它返回null,我尝试了不带$的情况,返回了25,不带.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. 我怎样才能得到一个变量作为第一个数字(25.00,带小数点)和一个不同的变量(100.00,带小数点)。 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. 这是在javascript中。

edit: here is my function, it reads a .txt file and gets the string. 编辑:这是我的功能,它读取一个.txt文件并获取字符串。 I can console.log the string just fine and it work, but it won't work when I use $ in regex! 我可以用console.log这个字符串很好地工作,但是当我在正则表达式中使用$时,它将不起作用!

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);
}

First of all \\d+ do not match . 首先\\d+不匹配.

You can use this 你可以用这个

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

Explanation 说明

  • \\d+ - Matches one or more digits. \\d+ -匹配一个或多个数字。
  • (\\.\\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)) 

 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. 然后将结果存储在数组arr中。 Then the values stored in the array are assigned to variables num1 and num2 through array destructuring. 然后,通过数组解构将数组中存储的值分配给变量num1和num2。

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. 如果您忽略修整末期的字符串s ,则正则表达式将与该字符串不匹配,从而导致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. 该模式继续匹配另一个字符(在本例中为斜杠),然后匹配一个美元符号,后面跟一个或多个数字,接着是一个句点,再跟一个或多个数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM