简体   繁体   English

使用正则表达式替换 email 字符串中倒数第二个字符(点)

[英]Replace second last occurrence of a char(dot) in an email string using regex

Please i would love to replace the second last occurrence of a char in a, the length of the strings can vary but the delimiter is always same I will give some examples below and what I have tried Input 1: james.sam.uri.stackoverflow.com请我想替换 a 中倒数第二个出现的字符,字符串的长度可能会有所不同,但分隔符始终相同我将在下面给出一些示例以及我尝试过的输入 1:james.sam.uri.stackoverflow .com

Output 1: james.sam.uri@stackoverflow.com Output 1:james.sam.uri@stackoverflow.com

Input 2: noman.stackoverflow.com输入 2:noman.stackoverflow.com

Output 2: noman@stackoverflow.com Output 2:noman@stackoverflow.com

Input 3: queen.elizabeth.empire.co.uk输入 3:queen.elizabeth.empire.co.uk

Output 3: queen.elizabeth@empire.co.uk Output 3:queen.elizabeth@empire.co.uk

My solution我的解决方案

//This works but I don't want this as its not a regex solution
const e = "noman.stackoverflow.com"
var index = e.lastIndexOf(".", email.lastIndexOf(".")-1)
return ${e.substring(0,index)}@${e.substring(index+1)}

Regex
e.replace(/\.(\.*)/, @$1)
//this works for Input 2 not Input 1, i need regex that would work for both, it only matches the first dot

The issue in the example data for the second last dot, is that the last example ends on .co.uk倒数第二个点的示例数据中的问题是最后一个示例以.co.uk结尾

One option for these specific examples could be using a pattern to exclude that specific part.这些特定示例的一个选项可能是使用模式来排除该特定部分。

(\S+)\.(?!co\.uk$)(\S*?\.[^\s.]+)$
  • (\S+) Capture group 1 , match 1+ non whitespace chars (\S+)捕获组 1 ,匹配 1+ 非空白字符
  • \.(?.co\.uk$) Match a . \.(?.co\.uk$)匹配一个. followed by a negative lookahead asserting directly to the right is not co.uk后跟一个直接向右的否定前瞻断言不是co.uk
  • ( Capture group 2 (捕获组 2
    • \S*?\. Match 0+ times a non whitspace char non greedy and then a .匹配 0+ 次非 whitspace char 非贪婪,然后是.
    • [^\s.]+ Match 1+ times a non whitespace char except a . [^\s.]+匹配 1+ 次非空白字符,除了.
  • ) Close group 2 )关闭第 2 组
  • $ End of string $字符串结尾

See a regex demo .查看正则表达式演示

 [ "james.sam.uri.stackoverflow.com", "noman.stackoverflow.com", "queen.elizabeth.empire.co.uk" ].forEach(s => console.log(s.replace(/(\S+)\.(?.co\?uk$)(\S*.\.[^\s,]+)$/; "$1@$2")) );

Here's another approach:这是另一种方法:

(\S+)\.(\S+\.\S{3,}?)$
       (            )$  At the end of the string, capture by
             \S{3,}?    lazily matching 3+ non-whitespace characters
        \S+\.           and any non-whitespace characters with period in front.
(\S+)\.                 Also capture anything before the separating period.

Notably, it would fail for an email like test.stackoverflow.co.net .值得注意的是,对于像test.stackoverflow.co.net这样的 email 来说,它会失败。 If that format is a requirement, I'd recommend a different approach.如果该格式是必需的,我会推荐一种不同的方法。

 [ "james.sam.uri.stackoverflow.com", "noman.stackoverflow.com", "queen.elizabeth.empire.co.uk", "test.stackoverflow.co.net" ].forEach(s => console.log(s.replace(/(\S+)\.(\S+\.\S{3,}?)$/, "$1@$2")) );

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

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