How to remove *.*, ** from string in JavaScript - Stack Overflow

I need to remove fractional addresses from a street address input line. I need to allow for people who

I need to remove fractional addresses from a street address input line. I need to allow for people who enter strings like the following:

  • 1600 1/2 Main St
  • 1600 3/4 Main Street
  • 1600.5 Main St
  • 1600.75 3rd Street

The above examples should be output as follows:

  • 1600 Main St
  • 1600 Main Street
  • 1600 Main St
  • 1600 3rd Street

Here's what I've started with:

var ele = document.getElementById('billing_address_1');

if (typeof(ele) != 'undefined' && ele != null) {
    function fractionalAddress() {
        originalStreet = ele.value;

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

I haven't quite figured out how to grab the character after the slash but before the space. So, the above converts "1600 3/4 Main Street" into "16004 Main Street." Close, but not quite there!

What am I missing? Would it be better to split this into an array?

I need to remove fractional addresses from a street address input line. I need to allow for people who enter strings like the following:

  • 1600 1/2 Main St
  • 1600 3/4 Main Street
  • 1600.5 Main St
  • 1600.75 3rd Street

The above examples should be output as follows:

  • 1600 Main St
  • 1600 Main Street
  • 1600 Main St
  • 1600 3rd Street

Here's what I've started with:

var ele = document.getElementById('billing_address_1');

if (typeof(ele) != 'undefined' && ele != null) {
    function fractionalAddress() {
        originalStreet = ele.value;

        //https://stackoverflow./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);
}

I haven't quite figured out how to grab the character after the slash but before the space. So, the above converts "1600 3/4 Main Street" into "16004 Main Street." Close, but not quite there!

What am I missing? Would it be better to split this into an array?

Share Improve this question asked May 3, 2018 at 14:54 paulmiller3000paulmiller3000 46110 silver badges27 bronze badges 2
  • i thing you will need lookaheads – zedling Commented May 3, 2018 at 14:56
  • weird you would remove that from a valid address..... Add numbers to your regexp – epascarello Commented May 3, 2018 at 15:03
Add a ment  | 

2 Answers 2

Reset to default 5

This works for me with the help of regex101.

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,"")
)

breakdown:

This regex seems to work.

finalStreet = originalStreet.replace(/\s*(\d+\/\d+|\.\d+)\s/g, '');   

A tip for the future, you can use a site like regex101. to test and edit your regex

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742372740a4431581.html

相关推荐

  • How to remove *.*, ** from string in JavaScript - Stack Overflow

    I need to remove fractional addresses from a street address input line. I need to allow for people who

    1天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信