我有一个格式为 的字符串some words @first.last more words @first.last。我想编写一个正则表达式来挑选格式中的任何子字符串,@first.last以便我可以用其他内容替换该子字符串。此正则表达式应仅考虑@first.last子字符串并忽略@符号前面的任何字符或last包含该空格后第一个空格之后的任何字符。前任:
regex = new RegExp(/[^\[\s](@[a-zA-Z0-9\.\-]+)/im);
str = 'Hey @first.last tell [@first.last] to check this out';
str = str.replace(regex, 'Keanu');
/** str: 'Hey Keanu tell [@first.last] to check this out? **?
我尝试过的正则表达式:
(@[a-zA-Z0-9\.\-]+)-> 会让我在那里做一部分,但不会在@符号之前去掉字符
[^\[](@[a-zA-Z0-9\.\-]+)-> 如果@first.last是字符串的第一个子字符串,则此正则表达式失败,即。@first.last look at this不会被 str.replace 调用改变
[^\[\s](@[a-zA-Z0-9\.\-]+) -> 尝试过滤掉前导空格
[^.+?](@[a-zA-Z0-9\.\-]+)
让我感到困惑的主要事情是在 之前包含什么,(@[a-zA-Z0-9\.\-]+)以确保我只检测到@符号和紧跟在 first.last 格式之后的字符。
我感谢任何帮助和协助。
慕少森
相关分类