简体   繁体   English

如何使用javascript获取电话号码中的特定号码

[英]How to get a specific number in a phone number using javascript

I would like to get the following number after the first "7" in a phone number. 我想在电话号码的第一个“ 7”之后得到以下号码。 Let's say "077777777" its "7" and "0788787878" its "8" and "0727923743" is "2" etc how can i do this in javascript i was thinking of something like this 假设“ 077777777”是“ 7”,“ 0788787878”是“ 8”,“ 0727923743”是“ 2”,等等,我该如何在javascript中进行此操作?

 var phone = 0784848236.substring(0784848236.indexOf("7")); 

but its far different. 但是却大不相同。

You could use a regular expression: 您可以使用正则表达式:

 var match = '0784848236'.match(/7(.)/); match = match && match[1]; console.log(match); 

NB: This will result in null when there is no match. 注意:如果没有匹配项,则结果为null

Note that it is important to have your phone number in string format. 请注意,电话号码必须为字符串格式,这一点很重要。 If you use a number data type, and have it converted to string on the fly, you'll loose prepadded zeroes. 如果您使用数字数据类型,并且将其即时转换为字符串,则将丢失前置的零。 See how looking for a zero fails in this case: 看看在这种情况下寻找零是如何失败的:

 var phone = 0784848236; var match = String(phone).match(/0(.)/); match = match && match[1]; console.log(match); // null !!! The zero is lost. 

Your code is close, but has two issues: 您的代码很接近,但是有两个问题:

  1. 0784848236 is an int, not a string, so you cannot call substring() or indexOf() on it. 0784848236是一个整数,而不是字符串,因此无法在其上调用substring()indexOf() You should instead use "0784848236" (in quotes). 您应该改用"0784848236" (用引号引起来)。
  2. substring() gives you the entire string after the specified index, but you only want the first character of that substring. substring()为您提供指定索引后的整个字符串,但是您只需要该子字符串的第一个字符。 So you don't actually need to use substring() here at all. 因此,您实际上根本不需要在这里使用substring() Instead, you can just use direct access (index into). 相反,您可以只使用直接访问 (索引到)。

Instead, try something like this: 而是尝试这样的事情:

var phone = "0784848236"["0784848236".indexOf("7") + 1];

In order to use the substring method, you need to pass in a string. 为了使用substring方法,您需要传入一个字符串。

You also need to provide a start and end index. 您还需要提供开始索引和结束索引。 Here is the reference for the method: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring str.substring(indexStart[, indexEnd]) 这是该方法的参考: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/substring str.substring(indexStart [,indexEnd])

If I was to change your question by declaring a string from the phone number then I could use substring on the string. 如果要通过从电话号码中声明一个字符串来更改您的问题,则可以在该字符串上使用子字符串。 Like this: 像这样:

var pn = '0784848236';
var n = pn.substring(pn.indexOf("7") + 1, pn.indexOf("7") + 2);

Now the result will be the next number following the '7' 现在结果将是“ 7”之后的下一个数字

to get the following number after the first "7" in a phone number 在电话号码的第一个“ 7”之后获得以下号码

Here are two approaches: 这是两种方法:

String.prototype.substr() and String.prototype.indexOf() functions approach: String.prototype.substr()String.prototype.indexOf()函数的方法是:

 var phone = 0784848236, i, strPhone = String(phone), // string representation of phone number nextDigit = ((i = strPhone.indexOf("7")) !== -1)? strPhone.substr(i + 1, 1) : null; console.log(nextDigit); 


String.prototype.split() and Array.prototype.indexOf() functions approach: String.prototype.split()Array.prototype.indexOf()函数的方法是:

 var phone = 0784848236, arr = (''+0784848236).split(''), i, nextDigit = ((i = arr.indexOf("7")) !== -1)? arr[i+1] : null; console.log(nextDigit); 

Here's what's working for me: 这是为我工作的东西:

var num="0784848236";
var numberSeven = num.indexOf("7");

var yourNumber = num.substring((numberSeven + 1), (numberSeven + 2));

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

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