【问题标题】:Regex doesn't work. What's wrong with my code? Can anyone help me fix this正则表达式不起作用。我的代码有什么问题?谁能帮我解决这个问题
【发布时间】:2021-04-04 04:35:35
【问题描述】:

我的代码:

property_unit_plan.post('/bulkAdd',(req, res) =>{
    Array.prototype.forEach.call(req.body, element => {
        db.sequelize.query('CALL sp_property_unit_plan_add_bulk( :unit_size_range, :no_of_bedrooms, :no_of_balcony, :no_of_washrooms, :unit_type, :is_hall, :unit_price, :bill_period)', {
            replacements: {
                unit_size_range: (element.unit_size_range).replace(/[,]+/g, ""),
                no_of_bedrooms: element.no_of_bedrooms,
                no_of_balcony: element.no_of_balcony,
                no_of_washrooms: element.no_of_washrooms,
                unit_type: element.unit_type,
                is_hall: element.is_hall,
                unit_price: (element.unit_price).replace(/[,]+/g, ""), 
                bill_period: element.bill_period
            }
        }).then(res => {
            res.send({ error: false, message: `Successfuly added.` });
        }).catch(err =>{
            res.send({ error: true, message: `Error 767: ${err}` });
        });
    });
});

返回值,unit_price 总是以逗号返回。

【问题讨论】:

    标签: javascript reactjs regex express


    【解决方案1】:

    为什么要使用正则表达式?您可以像这样转换为浮点数:

    parseFloat(element.unit_price.replaceAll(',','')),
    

    【讨论】:

    • 您必须使用 replaceAll() 或 RegEx。 replace() 将仅替换匹配字符串的第一次出现。例如:'220,100,300.200'.replace(',','') 将不起作用。
    • 你是对的,我已经更正了我的答案,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-13
    • 1970-01-01
    • 2011-08-15
    • 1970-01-01
    相关资源
    最近更新 更多