【问题标题】:Javascript Regex Function to Determine Whether String Contains Phone NumberJavascript Regex 函数确定字符串是否包含电话号码
【发布时间】:2021-03-29 08:02:40
【问题描述】:

我的任务是编写一个 JS 函数,如果字符串包含以下格式的电话号码,则返回 true:XXXXXXXXXX、XXX-XXX-XXXX、XXX XXX XXXX、(XXX) XXX-XXXX 和 (XXX)XXX -XXXX。

到目前为止我有这个:

function containsPhone(input) {
const regex = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;
return regex.test(input)
}

这仅在字符串仅包含电话号码时才有效,但我需要它来忽略字符串中的其他内容。例如,我需要它来工作

"电话号码是 (666) 666-6666"

不只是

“(666)666-6666”

任何帮助将不胜感激!

【问题讨论】:

    标签: javascript regex function


    【解决方案1】:

    只需删除开始和结束线锚点,你就可以开始了。

    \(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})

    Demo

    【讨论】:

      【解决方案2】:

      您可以在空格处拆分字符串,然后使用带有remove() 的辅助函数从每个值中删除任何不需要的符号。然后通过许多条件运行每个值,检查字符串中相邻兄弟的迭代值以重建您的值并运行返回 output

      const str = [
        'The phone number is not numbers XXX XXX-XXXX.',
        'The phone number is (555) 555-5555,',
        'The phone number is 555-555-5555;',
        'The phone number is 555 555 5555:',
        'The phone number is (555) 555-5555. ',
        'The phone number is (555)555-5555!',
        'The phone number is wrong format(555) e55-5555.'
      ]
      
      const containsPhone = (input) => {
        const regex = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;
        return regex.test(input)
      }
      
      const re = (string) => {
        return string.replace(/[.,:;{}*&%$#@!]/, '')
      }
      
      const check = (str) => {
        // set initial output to false
        let output = false;
        // initialize phone
        let phone;
        // split the string at white space
        const strings = str.split(' ')
        // run each value through forEach loop
        strings.forEach((val, i) => {
          // run val through helper replace function
          // to remove unwanted symbols from value
          val = re(val)
         
          // if the regex function does not return true &&
          // the value matches 3 numbers surrounded by () => (XXX)
          if (val.match(/^[(][0-9]{3}[)]/) && !containsPhone(val)) {
            // set the phone variable 
            // concat the val and its next value (XXX) XXX-XXXX
            // again removing any unwated following symbols re()
            phone = `${val}${re(strings[i+1])}`
          }
          // now we need to handle XXX XXX XXXX
          // just check values adjacent values to match number    
          if (strings[i + 1] !== undefined && strings[i + 1].match(/[0-9]+/) && val.match(/[0-9]+/) && strings[i - 1].match(/[0-9]+/)) {
            phone = re(`${strings[i - 1]} ${strings[i]} ${strings[i + 1]}`)
          }
          // check our two potential values using the regex function
          if (containsPhone(val) || containsPhone(phone)) {
            output = true
          }
        })
        // return the output
        return output;
      }
      
      str.forEach((str, i) => {
        console.log('str ' + Number(i + 1) + ': ' + check(str))
      })

      【讨论】:

        猜你喜欢
        • 2012-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-16
        • 2020-03-03
        • 2018-10-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多