简体   繁体   English

正则表达式 - 或 _ 在字符串的开头或结尾

[英]Regex for - or _ at beginning or end of string

I've been trying to check if my string has an underscore or hyphen at either the beginning or the end.我一直在尝试检查我的字符串在开头或结尾是否有下划线或连字符。

// pass
sdq
lorem ipsum
abc_def
harry-_potter


// catch
fg_
_asq
_dqw-
-asq-sq

Tried out the following but I'm learning regex so I'm not quite sure how to go forward from here:尝试了以下内容,但我正在学习正则表达式,所以我不太确定如何从这里开始:

/^[-_]?.*[-_]$/

^        => beginning anchor
[-_]?    => start with either - or _, optionally
.*       => match any number of any character (I believe I could have used ranges here)
[-_]$    => check for - or _ at the end

I also know that one possible solution could be to use |我也知道一种可能的解决方案是使用| , which is used for conditions, and what I need is to be able to check that: ,用于条件,我需要的是能够检查:

There is either a hypher or underscore at the beginning or end of the string.字符串的开头结尾有一个连字符或下划线。

How can I check this?我该如何检查? Can I use something other than |我可以使用除|其他东西吗? ? ?

Since OP has specifically asked this part:由于OP专门询问了这部分:

Can I use something other than |我可以使用除|其他东西吗? ? ?

Here is a regex solution that doesn't use alternation and uses negative lookahead to apply a condition that last character must not be _ or - :这是一个不使用交替并使用负前瞻来应用最后一个字符不能是_-的条件的正则表达式解决方案:

/^(?!.*[-_]$)[^-_].*/mg

RegEx Details:正则表达式详情:

  • (?!.*[-_]$) is a negative lookahead that means fail the match is - or _ are found just before end of line. (?!.*[-_]$)是一个否定的前瞻,这意味着匹配失败-_在行尾之前找到。
  • [^-_] is a negated character class that means match any character that is not - or _ . [^-_]是一个否定字符类,这意味着匹配任何不是-_字符。

RegEx Demo正则表达式演示

Having said that most simple and efficient solution would be what I wrote in my comment earlier ie using alternation:话虽如此,最简单有效的解决方案是我之前在评论中写的内容,即使用交替:

/^[-_]|[-_]$/mg

Use this regex to check and fail your mismatches.使用这个正则表达式来检查和失败你的不匹配。

You'll need to alternate.你需要交替。 Either match [-_] at the beginning (followed by other characters), or match any number of characters and then match [-_] at the end of the string:要么匹配开头的[-_] (后跟其他字符),要么匹配任意数量的字符,然后匹配字符串末尾的[-_]

^(?:[-_].*|.*[-_])$

https://regex101.com/r/e7ESHW/1 https://regex101.com/r/e7ESHW/1

The group isn't necessary , but I think it makes the intent of the pattern clearer (a match must match the full length of the string).该组不是必需的,但我认为它使模式的意图更清晰(匹配必须与字符串的全长匹配)。 You can leave it out if you want:如果您愿意,可以将其省略:

^[-_].*|.*[-_]$

If you don't care at all about what characters get matched, and only want to see if the string starts with / ends with a dash or underscore, then you can leave out the .* s.如果您根本不关心匹配哪些字符,而只想查看字符串以 / 开头是否以破折号或下划线结尾,那么您可以省略.* s。

Can I use something other than |?我可以使用 | 以外的东西吗?

Just in case you don't want to use regex, use startsWith and endsWith以防万一您不想使用正则表达式,请使用startsWithendsWith

 let arr = ['sdq', 'lorem ipsum', 'abc_def', 'harry-_potter', 'fg_', '_asq', '_dqw-', '-asq-sq'] let tester = (str) => { return !(str.startsWith('-') || str.startsWith('_') || str.endsWith('-') || str.endsWith('_')) } arr.forEach(str => console.log(str, '--->', tester(str)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM