简体   繁体   English

JavaScript函数中的RegEx问题-不替换任何内容

[英]RegEx issue in JavaScript Function - Not replacing anything

Plan A: it's such a simple function... it's ridiculous, really. 计划A:这是一个非常简单的功能……真的很荒谬。 I'm either totally misunderstanding how RegEx works with string replacement, or I'm making another stupid mistake that I just can't pinpoint. 我要么完全误解了RegEx如何与字符串替换一起工作,要么我犯了另一个我无法查明的愚蠢错误。

    function returnFloat(str){
        console.log(str.replace(/$,)( /g,""));
    }

but when I call it: 但是当我称它为:

    returnFloat("($ 51,453,042.21)")
    >>> ($ 51,453,042.21)

It's my understanding that my regular expression should remove all occurrences of the dollar sign, the comma, and the parentheses. 据我了解,我的正则表达式应删除所有出现的美元符号,逗号和括号。 I've read through at least 10 different posts of similar issues (most people had the regex as a string or an invalid regex, but I don't think that applies here) without any changes resolving my issues. 我已经通读了至少10篇类似问题的文章(大多数人使用正则表达式作为字符串或无效的正则表达式,但我认为这不适用于此处),而没有解决我的问题的任何更改。

My plan B is ugly: 我的计划B丑陋:

            str = str.replace("$", "");
            str = str.replace(",", "");
            str = str.replace(",", "");
            str = str.replace(" ", "");
            str = str.replace("(", "");
            str = str.replace(")", "");
            console.log(str);   

There are certain things in RegEx that are considered special regex characters, which include the characters $ , ( and ) . RegEx中有某些东西被认为是特殊的正则表达式字符,其中包括字符$() You need to escape them (and put them in a character set or bitwise or grouping) if you want to search for them exactly. 如果要精确搜索它们,则需要对其进行转义(并将它们放入字符集或按位或分组)。 Otherwise Your Regex makes no sense to an interpreter 否则, 您的Regex对解释器毫无意义

 function toFloat(str){ return str.replace(/[\\$,\\(\\)]/g,''); } console.log(toFloat('($1,234,567.90')); 

Please note that this does not conver this string to a float, if you tried to do toFloat('($1,234,567.90)')+10 you would get '1234568.9010'. 请注意,这并不是这个字符串CONVER的浮动,如果你试图做toFloat('($1,234,567.90)')+10 you would get '1234568.9010'. You would need to call the parseFloat() function. 您将需要调用parseFloat()函数。

$字符表示行尾,请尝试:

console.log(str.replace(/[\$,)( ]/g,""));

You can fix your replacement as .replace(/[$,)( ]/g, "") . 您可以将替换项固定为.replace(/[$,)( ]/g, "")

However, if you want to remove all letters that are not digit or dot, and easier way exists: 但是,如果要删除所有不是数字或点的字母,则存在更简单的方法:

.replace(/[^\d.]/g, "")

Here \\d means digit (0 .. 9), and [^\\d.] means "not any of the symbols within the [...] ", in this case not a digit and not a dot. 这里\\d指数(0。9),和[^\\d.]指“未任何符号的内[...] ”,在这种情况下不是数字,而不是一个点。

if i understand correctly you want to have this list : 51,453,042.21 如果我理解正确,那么您希望拥有此列表:51,453,042.21

What you need are character classes. 您需要的是角色类。 In that, you've only to worry about the ], \\ and - characters (and ^ if you're placing it straight after the beginning of the character class "[" ). 这样,您只需要担心],\\和-字符(和^,如果您将其放在字符类“ [”的开头之后)就可以了。 Syntax: [characters] where characters is a list with characters to be drop( in your case $() ). 语法:[characters],其中characters是一个列表,其中包含要放置的字符(在您的情况下为$())。
The g means Global, and causes the replace call to replace all matches, not just the first one. g表示全局,并导致replace调用替换所有匹配项,而不仅仅是第一个匹配项。

var myString = '($ 51,453,042.21)';
console.log(myString.replace(/[$()]/g, "")); //51,453,042.21

if you want to delete ',' 如果要删除“,”

var myString = '($ 51,453,042.21)';
console.log(myString.replace(/[$(),]/g, "")); //51453042.21

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

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