Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match if a string contains another string preceded by dot, space or nothing and followed by space, hyphen or nothing

I want to check if a string starts, ends with the searched string or contains the searched strings preceded by:

  • one or more alphabet characters and a space

  • one or more alphabet characters plus a point

And followed by s, hyphen, or space and alphanumeric characters.

If the searched keyword is Hansen:

  • These are the cases that should match.
const shouldMatchCases = [
  "Hansen",
  "Studio Hansen"
  "Hansen-studio",
  "Hansen studio",
  "N.Hansen studio",
  "N.Hansen-studio",
  "N Hansens studio",
  "N Hansen studio",
];
  • These are the case that should not match.

const shouldNotMatchCases = [
  "Hansenssutdio",
  "Hansenstest",
  "Hansenxxx",
  "NormanHansen",
  ".Hansen-studio",
  "Hansen-"
];

I have tried this regex, but it does not matche Hansen and Studio Hansen and matches .Hansen-studio that it must not do.

  const regex = new RegExp(`(?:\\b|^)(?:[A-Za-z]+\\.)?(${etternavn})s?(-|\\s)[A-Za-z0-9]+`)

function matchesPattern(str, keyword) {
  const regex = new RegExp(`(?:\\b|^)(?:[A-Za-z0-9]+\\.)?(${keyword})s?(-|\\s)[A-Za-z0-9]+`)
  return regex.test(str);
}
// Test cases that should match
const shouldMatchCases = [
  "Hansen",
  "Test Hansen",
  "Hansen-studio",
  "Hansen studio",
  "N.Hansen studio",
  "N.Hansen-studio",
  "N Hansens studio",
  "N Hansen studio",
];
// Test cases that should not match
const shouldNotMatchCases = [
  "Hansenssutdio",
  "Hansenstest",
  "Hansenxxx",
  "NormanHansen",
  ".Hansen-studio",
  "Hansen-"
];
// Test with dynamic keyword
const keyword = "Hansen";
console.log(`Testing with keyword: '${keyword}' for expected matches:`);
shouldMatchCases.forEach(str => {
  console.log(`${str}: ${matchesPattern(str, keyword)}`);
});
console.log(`\nTesting with keyword: '${keyword}' for expected non-matches:`);
shouldNotMatchCases.forEach(str => {
  console.log(`${str}: ${matchesPattern(str, keyword)}`);
});
like image 260
mahan Avatar asked Nov 15 '25 22:11

mahan


1 Answers

You can use

new RegExp(String.raw`(?<=\p{L}(?:\s+|\.\s*)|^)${keyword}(?:s?[-\s]\p{L}|$)`, `u`)

See the regex demo. Mind the u flag.

Details

  • (?<=\p{L}(?:\s+|\.\s*)|^) - at the start of string (^), or right after a Unicode letter (\p{L}) that is followed with either one or more whitespaces (\s+) or a dot and zero or more whitespaces (\.\s*)
  • Hansen - the keyword and then
  • (?:s?[-\s]\p{L}|$) - an optional s and then either a - or whitespace and a Unicode letter (s?[-\s]\p{L}), or end of string ($).

function matchesPattern(str, keyword) {
  const regex = new RegExp(String.raw`(?<=\p{L}(?:\s+|\.\s*)|^)${keyword}(?:s?[-\s]\p{L}|$)`, `u`)
  return regex.test(str);
}
// Test cases that should match
const shouldMatchCases = [
  "Hansen",
  "Test Hansen",
  "Hansen-studio",
  "Hansen studio",
  "N.Hansen studio",
  "N.Hansen-studio",
  "N Hansens studio",
  "N Hansen studio",
];
// Test cases that should not match
const shouldNotMatchCases = [
  "Hansenssutdio",
  "Hansenstest",
  "Hansenxxx",
  "NormanHansen",
  ".Hansen-studio",
  "Hansen-",
  "Hansens"
];
// Test with dynamic keyword
const keyword = "Hansen";
console.log(`Testing with keyword: '${keyword}' for expected matches:`);
shouldMatchCases.forEach(str => {
  console.log(`${str}: ${matchesPattern(str, keyword)}`);
});
console.log(`\nTesting with keyword: '${keyword}' for expected non-matches:`);
shouldNotMatchCases.forEach(str => {
  console.log(`${str}: ${matchesPattern(str, keyword)}`);
});
like image 81
Wiktor Stribiżew Avatar answered Nov 17 '25 16:11

Wiktor Stribiżew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!