只检测以空格开头的连字符的正则表达式

huangapple go评论46阅读模式
英文:

Regex that detects only hyphen start with space

问题

我需要一个正则表达式来匹配以空格开头的连字符,例如 -,而不是 -。 应匹配以下内容:

- this is what I want!
英文:

I need a regex for hypens that start with whitespace like -, not -:

- hello, - this is what I want!

... should match:

- this is what I want!

答案1

得分: 1

请看这个例子:

console.log(/^(?=.* -)(?=.*[a-zA-Z])/.test(' -hello')); // true
英文:

Try this one-

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

console.log(/^(?=.* -)(?=.*[a-zA-Z])/.test(&#39; -hello&#39;)); // true

<!-- end snippet -->

答案2

得分: 1

你没有提及你的语言,但这个正则表达式在几乎所有语言中都可以使用:

/\s-.*?($|,)/
英文:

You didn't mention you lang, but this will work in almost languages:

/\s-.*?($|,)/

huangapple
  • 本文由 发表于 2023年3月12日 15:26:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711634.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定