我需要从街道地址输入行中删除小数地址。我需要允许输入字符串的人如下:
以上示例应输出如下:
以下是我开始使用的内容:
var ele = document.getElementById('billing_address_1');
if (typeof(ele) != 'undefined' && ele != null) {
function fractionalAddress() {
originalStreet = ele.value;
//https://stackoverflow.com/questions/4328500/how-can-i-strip-all-punctuation-from-a-string-in-javascript-using-regex
//var punctuationless = originalStreet.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"");
//var finalStreet = punctuationless.replace(/\s{2,}/g," ");
finalStreet = originalStreet.replace(/\s.*\//, '');
ele.value = finalStreet;
console.log(originalStreet);
console.log(finalStreet);
};
ele.addEventListener("change", fractionalAddress);
}
我还没有想到如何在斜线之后但在空间之前抓住角色。因此,以上转换" 1600 3/4 Main Street"进入" 16004 Main Street。"关闭,但不完全在那里!
我错过了什么?将它拆分成数组会更好吗?
答案 0 :(得分:4)
var addresses = `1600 1/2 Main St
1600 3/4 Main Street
1600.5 Main St
1600.75 3rd Street`
console.log(
addresses.replace(/ ?\d\/\d?|\.\d{1,}/g,"")
)

击穿:
答案 1 :(得分:1)
这个正则表达似乎有效。
finalStreet = originalStreet.replace(/\s*(\d+\/\d+|\.\d+)\s/g, '');
未来的提示,您可以使用像regex101.com这样的网站来测试和编辑您的正则表达式