我正在寻求帮助,以提出一个正则表达式,它将匹配票号字(任意数字后跟数字),而不是以#SN-开头的字符。 有任何想法吗?
示例字符串:
A string with has ref like #SN-INC0000058 and simple mention like INC0000059.RegEx比赛: INC0000059
我目前停留在这个与'#SN-'后面的数字匹配的分类,而不是排除整个单词/字符串。 我怀疑 - 被视为一个单词突破......任何想法?
/(?!#SN-)([A-Z]+[0-9]+)\b/ghttps://www.regexpal.com/?fam=99443
I am looking for assistance in coming up with a regular expression which will match ticket number words (any number of characters followed by digits) but not ones that begin with #SN-. Any ideas?
Example String:
A string with has ref like #SN-INC0000058 and simple mention like INC0000059.RegEx Matches: INC0000059
I am currently stuck at this breakdown which is matching the number after '#SN-' rather than excluding the entire word/string. I suspect - is being treated as a word break... any ideas?
/(?!#SN-)([A-Z]+[0-9]+)\b/ghttps://www.regexpal.com/?fam=99443
最满意答案
由于Javascript正则表达式不具备lookbehind功能,因此无法通过简单模式捕捉您想要的内容。 但是,在替换上下文中,可以使用函数作为替换参数和模式来系统地尝试捕获不想要的部分,从而可以轻松地处理此问题:
var result = yourstr.replace(/(#SN-)?\b[A-Z]+[0-9]+\b/g, function(m,g1) { return g1 ? m : 'yourreplacement'; });当捕获组1被定义时,函数返回整个匹配,否则返回替换字符串。
请注意,在函数内部,您无法使用$&或$1..$n占位符作为整个匹配或捕获组。 您必须使用函数参数来计算它们。
只有更严格,如果在目标之前描述所有可能性,也可以在没有回调函数的情况下完成,但是写入会很痛苦,并且效率不高,因为模式以替代方式开始:
var result = yourstring.replace(/(^|[^-]|(?:[^N]|^)-|(?:[^S]|^)N-|(?:^|[^#])SN-)\b[A-Z]+[0-9]+\b/g, '$1yourreplacement');Since Javascript regex doesn't have the lookbehind feature, there's no way to catch what you want with a simple pattern. However, in a replacement context, you can easily handle this using a function as replacement parameter and a pattern that systematically tries to catch the part you don't want:
var result = yourstr.replace(/(#SN-)?\b[A-Z]+[0-9]+\b/g, function(m,g1) { return g1 ? m : 'yourreplacement'; });When the capture group 1 is defined, the function returns the whole match, otherwise it returns the replacement string.
Note that inside the function you can't use the $& or $1..$n placeholders for the whole match or capture groups. You have to use the function parameters to figure them.
Only to be more rigorous, it can also be done without a callback function if you describe all possibilities before your target, but it's a pain to write and it isn't efficient since the pattern starts with an alternation:
var result = yourstring.replace(/(^|[^-]|(?:[^N]|^)-|(?:[^S]|^)N-|(?:^|[^#])SN-)\b[A-Z]+[0-9]+\b/g, '$1yourreplacement');正则表达式排除以哈希标记开头的单词(Regex to exclude words beginning with hash tag)我正在寻求帮助,以提出一个正则表达式,它将匹配票号字(任意数字后跟数字),而不是以#SN-开头的字符。 有任何想法吗?
示例字符串:
A string with has ref like #SN-INC0000058 and simple mention like INC0000059.RegEx比赛: INC0000059
我目前停留在这个与'#SN-'后面的数字匹配的分类,而不是排除整个单词/字符串。 我怀疑 - 被视为一个单词突破......任何想法?
/(?!#SN-)([A-Z]+[0-9]+)\b/ghttps://www.regexpal.com/?fam=99443
I am looking for assistance in coming up with a regular expression which will match ticket number words (any number of characters followed by digits) but not ones that begin with #SN-. Any ideas?
Example String:
A string with has ref like #SN-INC0000058 and simple mention like INC0000059.RegEx Matches: INC0000059
I am currently stuck at this breakdown which is matching the number after '#SN-' rather than excluding the entire word/string. I suspect - is being treated as a word break... any ideas?
/(?!#SN-)([A-Z]+[0-9]+)\b/ghttps://www.regexpal.com/?fam=99443
最满意答案
由于Javascript正则表达式不具备lookbehind功能,因此无法通过简单模式捕捉您想要的内容。 但是,在替换上下文中,可以使用函数作为替换参数和模式来系统地尝试捕获不想要的部分,从而可以轻松地处理此问题:
var result = yourstr.replace(/(#SN-)?\b[A-Z]+[0-9]+\b/g, function(m,g1) { return g1 ? m : 'yourreplacement'; });当捕获组1被定义时,函数返回整个匹配,否则返回替换字符串。
请注意,在函数内部,您无法使用$&或$1..$n占位符作为整个匹配或捕获组。 您必须使用函数参数来计算它们。
只有更严格,如果在目标之前描述所有可能性,也可以在没有回调函数的情况下完成,但是写入会很痛苦,并且效率不高,因为模式以替代方式开始:
var result = yourstring.replace(/(^|[^-]|(?:[^N]|^)-|(?:[^S]|^)N-|(?:^|[^#])SN-)\b[A-Z]+[0-9]+\b/g, '$1yourreplacement');Since Javascript regex doesn't have the lookbehind feature, there's no way to catch what you want with a simple pattern. However, in a replacement context, you can easily handle this using a function as replacement parameter and a pattern that systematically tries to catch the part you don't want:
var result = yourstr.replace(/(#SN-)?\b[A-Z]+[0-9]+\b/g, function(m,g1) { return g1 ? m : 'yourreplacement'; });When the capture group 1 is defined, the function returns the whole match, otherwise it returns the replacement string.
Note that inside the function you can't use the $& or $1..$n placeholders for the whole match or capture groups. You have to use the function parameters to figure them.
Only to be more rigorous, it can also be done without a callback function if you describe all possibilities before your target, but it's a pain to write and it isn't efficient since the pattern starts with an alternation:
var result = yourstring.replace(/(^|[^-]|(?:[^N]|^)-|(?:[^S]|^)N-|(?:^|[^#])SN-)\b[A-Z]+[0-9]+\b/g, '$1yourreplacement');
发布评论