字符串之间的正则表达式匹配
Regex Match in between Strings
我使用 Regex 提取格式为 [a-zA-z][0-9]{8} 的模式 Ex:K12345678
我需要从字符串中提取这个模式,这个模式应该正确匹配
我尝试了以下但我的测试用例如果在这种情况下失败
这是我的正则表达式 /[a-zA-Z][0-9]{8}/g
phonne number:9978434276K12345678:我的图案
对于这种情况,它失败了。
我的示例代码
const expression = /[a-zA-Z][0-9]{8}/;
const content = "phone number:9978434276K12345678:My pattern"
let patternMatch = content.match(expression);
预期的输出是 K12345678.The 我写的正则表达式不处理这个问题。
您可以使用 String.match(正则表达式)
"9978434276K12345678".match(/[a-zA-Z][0-9]{8}/)
它 return 是一个包含 4 个元素的数组:[String coincidence
,index
:数字,input
:字符串,groups
:未定义]
只保留元素 0:coincidence
和 1:index of the match
。
并使用它来检查字符串是否至少匹配一个
/Regex/.test(String)
/[a-zA-Z][0-9]{8}/.test("9978434276K12345678")
它将return是或否
USE 不带引号的表达式
const expression = /[a-zA-Z][0-9]{8}/;
const content = "phone number:9978434276K12345678:My pattern"
let patternMatch = content.match(expression);
我使用 Regex 提取格式为 [a-zA-z][0-9]{8} 的模式 Ex:K12345678 我需要从字符串中提取这个模式,这个模式应该正确匹配 我尝试了以下但我的测试用例如果在这种情况下失败
这是我的正则表达式 /[a-zA-Z][0-9]{8}/g phonne number:9978434276K12345678:我的图案
对于这种情况,它失败了。
我的示例代码
const expression = /[a-zA-Z][0-9]{8}/;
const content = "phone number:9978434276K12345678:My pattern"
let patternMatch = content.match(expression);
预期的输出是 K12345678.The 我写的正则表达式不处理这个问题。
您可以使用 String.match(正则表达式)
"9978434276K12345678".match(/[a-zA-Z][0-9]{8}/)
它 return 是一个包含 4 个元素的数组:[String coincidence
,index
:数字,input
:字符串,groups
:未定义]
只保留元素 0:coincidence
和 1:index of the match
。
并使用它来检查字符串是否至少匹配一个
/Regex/.test(String)
/[a-zA-Z][0-9]{8}/.test("9978434276K12345678")
它将return是或否
USE 不带引号的表达式
const expression = /[a-zA-Z][0-9]{8}/;
const content = "phone number:9978434276K12345678:My pattern"
let patternMatch = content.match(expression);