如何使用正则表达式将输入“123456789”格式化为传真/123456789@faxabc.com

How to format the input "123456789" to fax/123456789@faxabc.com using regular expression?

本文关键字:com 123456789@faxabc 格式化 正则表达式 何使用 输入 123456789      更新时间:2023-09-26

用户输入1234567890,然后需要将其格式化为传真/123456789@faxabc.com。已尝试以下正则表达式,但不起作用:

fax/{'d+}/@fax.com
fax'//{'d+}/@faxabc.com
[fax'//{'d+}/@faxabc.com]

最接近的是fax/{'d+}/@fax.com,将得到 fax123456789@faxabc.com。但是,需要在单词传真后使用"/"。

也许您正在尝试在字符串中匹配它,以便:

function formatFaxNo(s) {
  var re = /('D)('d+)('D)/;
  return s.replace(re, '$1' + 'fax/'+'$2'+'@faxabc.com' + '$3');
}
// Here is a fax/123456789@faxabc.com number.
formatFaxNo('Here is a 123456789 number.'); 
// Fax to here: fax/123456789@faxabc.com.
formatFaxNo('Fax to here: 123456789.');     

它被称为字符串操作/连接,而不是正则表达式:

    var input = 123456789; //or whatever user inputs
    var output = "fax/" + input + "@abc.com" ;

这有什么问题吗?