我有以下字符串,我必须从中提取用户名和 ID。
This is a string which has a @[User Full Name](contact:1) data inside.
要从上面的字符串中获取用户名和联系人 ID,我正在使用这个正则表达式模式。
var re = /\@\[(.*)\]\(contact\:(\d+)\)/;
text = text.replace(re,"username:$1 with ID: $2");
// result == username: User Full Name with ID: 1
它现在完美运行的问题是我在字符串中有多个用户名,我尝试使用 /g (全局)但它没有正确替换:示例字符串:
This is a string which has a @[User Full Name](contact:1) data inside. and it can also contain many other users data like @[Second Username](contact:2) and @[Third username](contact:3) and so many others....
当使用全局时,我得到这个结果:
var re = /\@\[(.*)\]\(contact\:(\d+)\)/g;
text = text.replace(re,"username:$1 with ID: $2");
//RESULT from above
This is a string which has a user username; User Full Name](contact:1) data inside. and it can also contain many other users data like @[[Second Username](contact:2) and @[Third username and ID: 52 and so many others....