RegExp#exec
and String#match
JS-D007166 break
167 }
168 /* `community-xx-speaker` */
169 case !!speakerRegex.exec(tag): {170 servAttribute = attributeList.get('community-language-speakers')
171 if (!servAttribute) throw new Error('Unable to locate attribute record for "language-speakers"')
172 const [, lang] = speakerRegex.exec(tag) ?? [undefined, '']
190 break
191 }
192 /* `service-{county|national|state}-xx...` */
193 case !!areaRegex.exec(tag): {194 const area = getAreaRecord(tag, helpers)
195 if (typeof area?.id !== 'string') return incompatible(tag)
196 areaAttribute = area as NonNullable<ServiceAreaRecord>
178 break
179 }
180 /* `lang-xx` */
181 case !!langRegex.exec(tag): {182 servAttribute = attributeList.get('languages-lang-offered')
183 if (!servAttribute) throw new Error('Unable to locate attribute record for "lang-offered"')
184 const [, lang] = langRegex.exec(tag) ?? [undefined, '']
RegExp#exec
and String#match
should only be used when we need to use the parts of a string that match a specific pattern:
const matches = /[a-zA-Z0-9]+/.exec(string)
for (const match of matches) {
processMatch(match)
}
If you only want to know whether a string matches a particular pattern, RegExp#test
is a faster alternative.
const matches = str.match(/[a-zA-Z0-9]/) ? process(str) : process("default-str");
const strMatchesPattern = !!str.match(/regex/)
const regexp = new RegExp("[a-zA-Z0-9]*")
if (regexp.exec(myString)) {
// ...
}
const matches = '/hasTheMagic/'.test(str) ? process(str) : process("default-str");
const strMatchesPattern = /regex/.test(str)
const regexp = new RegExp("[a-zA-Z0-9]*")
if (regexp.test(myString)) {
// ...
}