Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

227
Views
Regex pattern to match all words of a phrase while accepting quote (') in between but NOT in First or End of word

Been looking for a couple of days now and still could not get my head around it.

This is the phrase,

const phrase = `"That's the password: 'PASSWORD 123'!", cried the Special Agent.\nSo I fled.`;

and this is the expected transformation,

['that's', 'the', 'password', 'password', '123', 'cried', 'the', 'special', 'agent', 'so', 'i', 'fled']

The first element (that's) of the array is the problem area.

I can only get below transformation,

['thats', 'the', 'password', 'password', '123', 'cried', 'the', 'special', 'agent', 'so', 'i', 'fled']

Using below code

const cleanPhrase = phrase.replace(/["':!,.]/g, '').replace(/[\n]/g, ' ').toLocaleLowerCase()
const words = cleanPhrase.split(' ');

Is there a way to ignore the single quotes on 'Password 123' but accept the single quote on that's ?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

First i think it is better to use String.prototype.match() instead of split.
Then there is 2 simple methods for that:

A) Without using look-behind

const phrase = `"That's the password: 'PASSWORD 123'!", cried the Special Agent.\nSo I fled.`;
console.log(phrase.match(/(?!')[\w']*\w/g));

Live try

B) Using look-behind (Check browser compatibility)

const phrase = `"That's the password: 'PASSWORD 123'!", cried the Special Agent.\nSo I fled.`;
console.log(phrase.match(/(?!')[\w']+(?<!')/g));

Explaination
  • \w = [a-zA-Z0-9_]
  • [\w'] a character set/class + '
    • * Zero or more length (Of the set)
    • + One or more length (Of the set)
  • (?!') Check if in first of your ahead is not a '
  • (?<!') Check if in last of your behind is not a '

Note: In first method [\w']* can be zero or more so for checking ahead of that, i use a char length class (\w) without the quote ' to i can avoid of using negative look-behind and also support even one character words like I

about 4 years ago · Juan Pablo Isaza Report

0

I would first replace all quotes that are surrounded by letters with a "placeholder"... That is a character that should not appear in the string. I used a pipe (|) in the example below.

const phrase = `"That's the password: 'PASSWORD 123'!", cried the Special Agent.\nSo I fled.`;

const cleanPhrase = phrase

  // Replace all quotes with a placeholder
  .replace(/(\w)'(\w)/, "$1|$2")
  .replace(/["':!,.]/g, "")
  .replace(/[\n]/g, " ")

  // Restore the quotes where there is a placeholder
  .replace(/(\w)\|(\w)/, "$1'$2")
  .toLocaleLowerCase();
const words = cleanPhrase.split(" ");

console.log(words);

about 4 years ago · Juan Pablo Isaza Report

0

First replace all the symbols with an empty string, then replace ', ' and \n with a single space:

const phrase = `"That's the password: 'PASSWORD 123'!", cried the Special Agent.\nSo I fled.`;
const words = phrase.replace(/["!.:,]/g, '')
  .replace(/\s\'|\'\s|\n/g, ' ')
  .toLocaleLowerCase().split(' ');
console.log(words);

You could also use split instead of the second replace:

const phrase = `"That's the password: 'PASSWORD 123'!", cried the Special Agent.\nSo I fled.`;
const words = phrase.toLocaleLowerCase()
  .replace(/["!.:,]/g, '')
  .split(/\s\'|\'\s|\n|\s/g);
  console.log(words);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!