我要判断字符串是以搜索的字符串开头、以搜索的字符串结尾,还是包含以以下内容开头的搜索的字符串:

  • 一个或多个字母字符和空格

  • 一个或多个字母字符加上一个点

然后是S、连字符或空格和字母数字字符.

如果搜索的关键字是Hansen:

  • 这些都是应该匹配的案件.
const shouldMatchCases = [
  "Hansen",
  "Studio Hansen"
  "Hansen-studio",
  "Hansen studio",
  "N.Hansen studio",
  "N.Hansen-studio",
  "N Hansens studio",
  "N Hansen studio",
];
  • 这些都是不应该匹配的情况.

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

我试过这个正则表达式,但它不能匹配HansenStudio Hansen,也不能匹配.Hansen-studio.

  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)}`);
});

推荐答案

您可以使用

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

看看regex demo.第u章国旗

Details

  • (?<=\p{L}(?:\s+|\.\s*)|^)-在字符串(^)的开头,或紧跟在Unicode字母(\p{L})之后,该字母后面紧跟一个或多个空格(\s+)或一个圆点和零个或多个空格(\.\s*)
  • Hansen-关键字,然后
  • (?:s?[-\s]\p{L}|$)-可选的s,然后是-、空格和Unicode字母(s?[-\s]\p{L})或字符串结尾($).

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)}`);
});

Javascript相关问答推荐

如何在JavaScript中通过一次单击即可举办多个活动

脚本.js:3:20未捕获的类型错误:无法读取空的属性(读取addEventHandler)

Flisk和JS错误:未捕获的Syntax错误:意外的令牌'<'

如何编辑代码FlipDown.js倒计时?

我无法在NightWatch.js测试中获取完整的Chrome浏览器控制台日志(log)

从实时数据库(Firebase)上的子类别读取数据

Snowflake JavaScript存储过程返回成功,尽管预期失败

当点击注册页面上的注册按钮时,邮箱重复

Prisma具有至少一个值的多对多关系

在我的index.html页面上找不到我的Java脚本条形图

使用getBorbingClientRect()更改绝对元素位置

同一类的所有div';S的模式窗口

TypeError:无法读取未定义的属性(正在读取';宽度';)

无法读取未定义的属性(正在读取合并)-react RTK

在验证和提交表单后使用useNavigate()进行react 重定向,使用带有加载器和操作的路由

在GraphQL解析器中修改上下文值

Reaction即使在重新呈现后也会在方法内部保留局部值

如何在TransformControls模式下只保留箭头进行翻译?

我如何才能让p5.js在不使用实例模式的情况下工作?

当一条路由在Reaction路由中命中时,如何有条件地渲染两个组件或更改两个插座?