0

Can someone please enlighten me, I tried a lot of thing but this doesn't work. I'll give the example I'm getting an error:

let str = `First example (working): {Æ¢ Example phrase 123 áè -*, example Æ¢}
           Second example (not working): {Ƣ A SuperSlim notebook computer that's light on weight, heavy on features and performance *This .9 thin Notebook Computer weighs in at roughly 3 pounds - which might make you wonder how they cram such great features into that small package *10.4 XGA Active matrix screen with XBRITE display technology *Intel Pentium 300 MHz processor with MMX technology *64 MB of SDRAM is included (expandable to 128 MB maximum) *6.4 GB fixed Hard Drive for Data Storage *Touch Pad with pen operation *One Type II PC card slot with Cardbus Zoomed Video support *External 1.44 MB floppy disk drive *integrated 56K V.90 Data/fax Modem for Internet access *512 KB MultiBank DRAM Cache memory *16-bit, Soundblaster compatible audio *MPEG1 Digital video that supports full screen playback *mono speakers *Built-in microphone *Infrared port *Responsive nearly full-size tactile Keyboard *programmable power key for unattended Email retrieval Please add $30 for shipping. Payment - western union. Ƣ}`

// Regex
console.log(
   str.match(/{Æ¢\s*([^(Æ¢)]*)\s*Æ¢}/g));

I am trimming the spaces and getting everything inside the curly brackets followed by "Æ¢" until "Æ¢}" The result is only the first example in the string, and it doesn't include the second:

> ["{Æ¢ Example phrase 123 áè -*, example Æ¢}"]
AlexSp3
  • 2,201
  • 2
  • 7
  • 24
FCLM
  • 95
  • 8

2 Answers2

0

For example:

const str = `First example (working): {Æ¢ Example phrase 123 áè -*, example Æ¢}
               Second example (not working): {Ƣ A SuperSlim notebook computer that's light on weight, heavy on features and performance *This .9 thin Notebook Computer weighs in at roughly 3 pounds - which might make you wonder how they cram such great features into that small package *10.4 XGA Active matrix screen with XBRITE display technology *Intel Pentium 300 MHz processor with MMX technology *64 MB of SDRAM is included (expandable to 128 MB maximum) *6.4 GB fixed Hard Drive for Data Storage *Touch Pad with pen operation *One Type II PC card slot with\n Cardbus Zoomed Video support *External 1.44 MB floppy disk drive *integrated 56K V.90 Data/fax Modem for Internet access *512 KB MultiBank DRAM Cache memory *16-bit, Soundblaster compatible audio *MPEG1 Digital video that supports full screen playback *mono speakers *Built-in microphone *Infrared port *Responsive nearly full-size tactile Keyboard *programmable power key for unattended Email retrieval Please add $30 for shipping. Payment - western union. Ƣ}`

const allMatches = str.matchAll(/{Æ¢\s*(.*?)\s*Æ¢}/gs);

for (let match of allMatches) {
  console.log(match[1]);
}

The s in the flags gs turns on "single-line" or "dotAll" mode so the . matches newline characters.

The ? means match "lazily", i.e. match as few characters as possible before the rest of the pattern can be matched.

When matching globally and using capture groups, matchAll or exec must be used.

MikeM
  • 13,156
  • 2
  • 34
  • 47
-1

Your second example includes characters that are found in your negated set:

[^(Æ¢)]

Specifically, the ( and ) characters.

If you remove those characters from the regular expression so that your left with:

{Æ¢\s*([^Æ¢]*)\s*Æ¢}

... then this will match your second example.

Shaz
  • 15,637
  • 3
  • 41
  • 59
  • `[^Æ¢]` means any single char other than `Æ` *or* `¢`. Negated character classes do not negate character *sequences*. To negate a char sequence, a tempered greedy token (or its unrolled variant) is required. However, here is just a case of matching strings between two strings. – Wiktor Stribiżew Aug 15 '21 at 19:36