简体   繁体   English

一个输入中的多个正则表达式

[英]multiple regexp in one input

hi i have one input type tel for mobile number and like use pattern for number format like this 938 119 1229 but when i use type tel i can use word in input i try use the two regex in one input but i don't know how:嗨,我有一种用于手机号码的输入类型 tel,并且喜欢使用这种数字格式的模式938 119 1229但是当我使用 tel 类型时,我可以在输入中使用单词,我尝试在一个输入中使用两个正则表达式,但我不知道如何:

function mobileNumInput(input){
    var regex = /[^0-9]/g;  //for digit only
    input.value=input.value.replace(/^(\d{3})(\d{3})(\d+)/, '$1 $2 $3');   //for space between numbers
    input.value=input.value.replace(regex)      

}

and html:和 html:

<input type="tel" name="phone"  placeholder="912 000 0000" maxlength="12"  min="0" max="9" pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}"  onkeyup="mobileNumInput(this)"  autofocus>

this is my placeholder: enter image description here这是我的占位符:在此处输入图像描述

i want this format: enter image description here我想要这种格式:在此处输入图像描述

But I don't want to use word like this: enter image description here sorry my english is bad但我不想用这样的词:在此处输入图片描述对不起我的英语不好

I'm guessing that you want to do the following:我猜你想要执行以下操作:

  1. Remove all non-numeric characters from the phone number.从电话号码中删除所有非数字字符。
    • Example: convert '(111)-174-1234' to '1111741234'示例:将'(111)-174-1234'转换为'1111741234'
  2. Add spaces in the correct location to the resultant number.在结果编号的正确位置添加空格。
    • Example: convert '1111741234' to '111 174 1234'示例:将'1111741234'转换为'111 174 1234'

If my assumptions about the purpose of this code are true, your code had two mistakes:如果我对这段代码用途的假设是正确的,那么您的代码有两个错误:

  1. You put step #1 after step #2.您将步骤 #1 放在步骤 #2 之后。
    • This led to an input '(111)-174-1234' having the regex (#2) running the following replacement: .replace(/^(\d{3})(\d{3})(\d+)/, '$1 $2 $3')这导致输入'(111)-174-1234'具有正则表达式 (#2) 运行以下替换: .replace(/^(\d{3})(\d{3})(\d+)/, '$1 $2 $3')
    • The above snippet of code only works on pure numbers.上面的代码片段仅适用于纯数字。 It does not recognize '(111)-174-1234' as containing any matches to /^(\d{3})(\d{3})(\d+)/ , so no replacements are made.不会'(111)-174-1234'识别为包含与/^(\d{3})(\d{3})(\d+)/的任何匹配项,因此不会进行替换。 In other words, after line 3 of your code runs, input.value has likely not changed.换句话说,在代码的第 3 行运行之后, input.value可能没有改变。
    • The solution to this is simply to switch line 3 and 4 in your program.解决这个问题的方法就是在程序中切换第 3 行和第 4 行。
  2. In step #1, you used .replace(regex) instead of .replace(regex,'') .在第 1 步中,您使用了.replace(regex)而不是.replace(regex,'')
    • This is just a String method technicality: String.prototype.replace accepts a regex and a string to replace it.这只是一个String方法的技术性: String.prototype.replace接受一个正则表达式和一个字符串来替换它。 Leaving the second parameter empty is the same as setting the second parameter as undefined .将第二个参数留空与将第二个参数设置为undefined相同。
    • An example is that "Hello world".replace(/l/g) is the same as "Hello world".replace(/l/g,undefined) .一个例子是"Hello world".replace(/l/g)"Hello world".replace(/l/g,undefined)相同。 The result of both of these snippets is "Heundefinedundefinedo world" .这两个片段的结果都是"Heundefinedundefinedo world" You can gain the desired behavior by using "Hello world".replace(/l/g,'') instead.您可以通过使用"Hello world".replace(/l/g,'')来获得所需的行为。 This will return "Heo world" .这将返回"Heo world"

I put my fixes into a revised version of your code:我将我的修复程序放入您的代码的修订版本中:

function mobileNumInput(input){
    var regex = /[^0-9]/g;  //for digit only
    input.value=input.value.replace(regex, ''); // Step #1 (remove non-digits)
    input.value=input.value.replace(/^(\d{3})(\d{3})(\d+)/, '$1 $2 $3');   //Step #2 (add spaces to valid phone number)
}

Here is a slightly further modified version of your code with one test:这是您的代码的稍微进一步修改的版本,其中包含一个测试:

function mobileNumInput(input){
    input.value=input.value
      .replace(/[^0-9]/g, '')//Remove all non-digits
      .replace(/^(\d{3})(\d{3})(\d+)/, '$1 $2 $3'); //Add spaces to the only-digit number 
}

function test(){
  var testInputElement=document.createElement("input");
  testInputElement.value='(123)-[456}  -  7890';//Horribly formatted phone number
  mobileNumInput(testInputElement);
  console.log(testInputElement.value);
}

test();
//123 456 7890

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

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