Test ID
Test ID

Reputation: 11

extract word from string usinfg regex in js

accusantique et o (MLS® R2608327). Sed ut perspiciatis voluptatem mls listing :MLS&reg#:12243235435 beatae vitae dicta MLS Number#:12243235435 sunt 3543654654675645654654 explicabo .

Given the string above how could I extract "(MLS® R2608327)"or"MLS&reg#:12243235435"or"MLS Number#:12243235435" in the string? value of it might change as this is dynamic. So, other instance might be "135435". The location/sequence might also change, it could be in the middle or last part.

This regex

^ ((MLS|mls|Mls|MLS&reg|mls&reg|Mls&reg|MLS®|MLS® Number|mls Number|Mls Number|mls®|Mls®|Property ID|)[^A-Za-z0-9])?(([^?*/\:;<> ]{1,23}\d{2,23}[^?*/\:;<> ]{1,23}) *)$

This regex is working well but it is not extracting mls:#w3432423 from a string like dfnjkgfkhgb mls:#234234245 fgjhfgf 3498234789 dshfdsfgbjhsf.

Upvotes: 0

Views: 71

Answers (1)

The fourth bird
The fourth bird

Reputation: 163577

Using a case insentive match, you can use:

\b(MLS(?:&reg|®?(?: Number)?)[^A-Za-z0-9]*)([^?*\/\\:;<> ]{1,23}\d{2,23}[^?*\/\\:;<> ]{1,23})

The pattern in parts matches:

  • \b A word boundary to prevent a partial match
  • ( Capture group 1
    • MLS Match literally
    • (?: Non capture group
      • &reg Match literally
      • | Or
      • ®?(?: Number)? Match optional ® and optional Number
    • ) Close non capture group
    • [^A-Za-z0-9]* Match optional chars other then the listed
  • ) Close group 1
  • ( Capture group 2
    • [^?*\/\\:;<> ]{1,23} Match 1-23 repetitions of any char except the listed
    • \d{2,23} Match 2 -23 digits
    • [^?*\/\\:;<> ]{1,23} Match 1-23 repetitions of any char except the listed
  • ) Close group 2

Regex demo

Upvotes: 0

Related Questions