[英]JavaScript: Regex for one letter and a point
Using Javascript, I am trying to split a text using a regular expression that contains only one letter and a point; 我试图使用Javascript使用仅包含一个字母和一个点的正则表达式拆分文本; for example 'A.'
例如“ A”。 or 'Q.'
或“ Q”。 I am using: array[0].split(/[AZ]+./);
我正在使用:array [0] .split(/ [AZ] +。/); But it is returning true if there are multiple characters like 'AA.'
但是,如果有多个字符,例如“ AA”,则返回true。 or 'ZZ.'
或“ ZZ”。
What is the regex for one character only? 仅一个字符的正则表达式是什么? I also have access to jQuery.
我也可以使用jQuery。
Thank you! 谢谢!
This should work: array[0].split(/([AZ]{1}\\.)/);
这应该起作用:
array[0].split(/([AZ]{1}\\.)/);
Explanation: 说明:
[AZ]{1}
- this looks for any single letter [AZ]{1}
-查找单个字母
\\.
- this looks for a .
-这寻找一个
.
note that you'll need the backslash as a .
请注意,您需要使用反斜杠作为
.
in a regex means any character. 正则表达式中的任何字符。
These are surrounded by brackets so that you are looking for the sequence: one letter followed by a .
这些用方括号括起来,以便您查找顺序:
one letter followed by a .
You'll need to remove the + as it means one or more. 您需要删除+,因为它意味着一个或多个。 if you want to find several occorances then you'll need the global flag (g at the end):
array[0].split(/([AZ]{1}\\.)/g);
如果要查找多个事件,则需要全局标志(末尾g):
array[0].split(/([AZ]{1}\\.)/g);
Limit to A. and not AA. 限制为A,而不是AA。
If you want to make sure its only one letter follow by a . 如果您想确保其只有一个字母,后跟一个。 and not match on the end of
AA.
并且在
AA.
末尾不匹配AA.
then you can use the word boundary like so: array[0].split(/\\b([AZ]{1}\\.)/);
那么您可以使用边界这样的单词:
array[0].split(/\\b([AZ]{1}\\.)/);
This site is amazing for helping with regex: https://regex101.com/ 这个站点对于正则表达式的帮助非常了不起: https : //regex101.com/
You don't want to use +
, as it means "one or more". 您不想使用
+
,因为它表示“一个或多个”。 Just remove it. 只需将其删除。 Also you have to escape the
.
另外,您还必须逃脱
.
otherwise it means "any character". 否则,它的意思是“任何字符”。
If you want to match only a capital letter and a point, this should do the trick : /[AZ]\\./g
if you want the regex to be case insensitive, simply add the i
flag : /[AZ]\\./gi
. 如果您只想匹配一个大写字母和一个点,这应该可以解决问题:/[
/[AZ]\\./g
如果希望正则表达式不区分大小写,只需添加i
标志:/[ /[AZ]\\./gi
。 Remove the g
flag if you only want to match the first occurrence. 如果只想匹配第一个匹配项,请删除
g
标志。
你可以试试这个
/\s[A-Z]\.\s/g
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.