简体   繁体   English

正则表达式匹配字符串末尾的子表达式

[英]regex to match subexpression at end of string

I'm trying to test whether the ending pattern in a string is an html closing tag (assuming trailing spaces are trimmed).我正在尝试测试字符串中的结束模式是否是 html 结束标记(假设尾随空格被修剪)。

var str1 = "<em>I</em> am <strong>dummy</strong> <em>text.</em>"; //ends with html close tag
var str2 = "<em>I</em> am <strong>dummy</strong> <strong>text.</strong>"; //ends with html close tag
var str3 = "<em>I</em> am <strong>dummy</strong> text"; //does not end with html close tag

Using str1 above, I would like to get the position of the ending tag, which is an.使用上面的str1,我想得到结束标签的position,这是一个。 Here are my attempts:这是我的尝试:

var rgx1 = /(<\/em>)$/g; // works. basic scenario. matches closing </em> tags at the end of the string.
var rgx2 = /<\s*\/\s*\w\s*.*?>/g; //matches html closing tags.
var rgx3 = /<\s*\/\s*\w\s*.*?>$/g; //doesn't work. supposed to match closing html tag at the end of the string

console.log(str.search(rgx1))

While rgx1 correctly returns the position of the ending tag, and rgx2 correctly returns the position of a closing html tag in general, I'm trying get a generalized regex that will return the positing of any html tag that ends the string. While rgx1 correctly returns the position of the ending tag, and rgx2 correctly returns the position of a closing html tag in general, I'm trying get a generalized regex that will return the positing of any html tag that ends the string. Why doesn't rgx3 work?为什么 rgx3 不起作用?

Should just use a negative char class to match anything that's not a closing >应该只使用负字符 class 来匹配任何不是结束的内容 >

var rgx = /<\/[^>]+>$/g;

as to why rgx3 didn't work... your pattern isn't really good but it should technically match... if it didn't work with the $ on the end there, then the string you are matching probably isn't trimmed as you suppose it to be (or some other thing on the end other than closing html tag)至于为什么 rgx3 不起作用......你的模式不是很好,但它应该在技术上匹配......如果它不能与最后的 $ 一起工作,那么你匹配的字符串可能不是按照您的设想进行修剪(或者除了关闭 html 标签之外的其他一些东西)

Seems like there might be an issue with rgx2 and rgx3 - an extra.*?似乎 rgx2 和 rgx3 可能存在问题 - 额外的。*? before the > and a missing * after \w - here's how I would write the regexes.在 > 之前和 \w 之后缺少 * - 这就是我编写正则表达式的方式。 The fact that rgx2 was working at all was because of the match all (.*) rgx2 工作的事实是因为 match all (.*)

var rgx2 = /<\s*\/\s*\w*\s*>/g;
var rgx3 = /<\s*\/\s*\w*\s*>$/g;

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

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