2

I'm new to regex and i don't find how to get text before ':' but not include it.

I do a replace in my code so i need to select all the text after ':'

Here is a demo:

/(?!.*:)./

Thank you for your help.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
amerej
  • 139
  • 1
  • 11

1 Answers1

2

It seems you need

const rx = /^[^:\r\n]+(?=:)/gm;
const str = `Hilary: YOU KNOW, THIS IS NOT WHY I CAME HERE.
Devon: DOESN'T MATTER.
Hilary: IT DOES. DEVON, WHEN I SAY THAT I'M TIRED OF SNEAKING AROUND, I MEANT IT.
Devon: DON'T GET UPSET, THOUGH.
Hilary: HOW CAN I NOT? I MEAN, LOOK AT WHAT I DID.
Devon: HONEY, HONEY, WE'RE IN LOVE WITH EACH OTHER. OKAY? AND WE KNOW EXACTLY WHAT WE HAVE TO DO. WE'RE GONNA TELL NEIL THE TRUTH.
Hilary: NOW?
Dr. Barrett: I THINK YOU CAN PROVIDE SOME VALUABLE INSIGHT INTO PHYLLIS NEWMAN'S RECENT STATE OF MIND.`;
document.body.innerHTML = "<pre>" + JSON.stringify(str.match(rx), 0, 4) + "</pre>";

Details

  • ^ - start of string
  • [^:\r\n]+ - one or more chars other than :, CR and LF
  • (?=:) - there must be : immediately to the right of the current location
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563