从正则表达式捕获组中删除空格

时间:2018-09-06 08:35:35

标签: regex

这应该是一项简单的工作,但是今天早上我似乎找不到我需要的答案

值:

paypal.Button.render({
        env: 'production', // sandbox | production
        client: {
            sandbox:    'mykey',
            production: 'mykey'
        },

        // Show the buyer a 'Pay Now' button in the checkout flow
        commit: true,

        // payment() is called when the button is clicked
        payment: function(data, actions) {

            // Make a call to the REST api to create the payment
            return actions.payment.create({
                payment: {
                    transactions: [
                        {
                            amount: { total: '5.00', currency: 'EUR' }
                        }
                    ]
                }
            });
        },

        // onAuthorize() is called when the buyer approves the payment
        onAuthorize: function(data, actions) {

            // Make a call to the REST api to execute the payment
            return actions.payment.execute().then(function() {
                window.location = "address";
            });
        }

    }, '#paypal-button-container');

当前正则表达式

N.123456 7

返回单个匹配组

N.(\d{6}\s?\d)

希望返回单个匹配组

123456 7

谢谢

2 个答案:

答案 0 :(得分:0)

您不能作为单个匹配组返回。我认为您要寻找的是非捕获组(?:)。

有解释here

也许这个正则表达式可以帮助您。它将排除具有非捕获组的空格字符。

N.(\d{6})(?:\s?)(\d)

它将捕获组1中的123456和组2中的7

您想要的可能是这个。它将返回1234567

"N.123456 7".replaceAll("N.(\\d{6})(?:\\s?)(\\d)", "$1$2")

答案 1 :(得分:-2)

尝试一下:

(?<=\d)\s+(?=\d+)

这应该有效。