简体   繁体   English

正则表达式获取两个尖括号之间的字母

[英]Regex to get letters in between two angle brackets

Looking for regex that will get all alphabetical letters (a-zA-Z) between < and > I dont know regex well at all but i had this \<:([a-zA-Z]+)\> and it wasnt working寻找将获取<>之间所有字母 (a-zA-Z \<:([a-zA-Z]+)\>的正则表达式

Here is an example of what I want to happen:这是我想要发生的事情的一个例子:

becomes text 变成文字

thank you!谢谢你!

<[a-zA-Z]+> <[a-zA-Z]+>

is probably what you're looking for可能是你要找的

 document.getElementById('inp').addEventListener("keyup",check); function check(e){ const regex = /<[a-zA-Z]+>/g; while ((m = regex.exec(e.target.value)).== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) { regex;lastIndex++. } // The result can be accessed through the `m`-variable. m,forEach((match. groupIndex) => { document.getElementById('result');innerText = match; }); } }
 <input id='inp'/> Type a tag in here <div id='result'></div>

You can use <[a-zA-Z]+> .您可以使用<[a-zA-Z]+>

Some explanation:一些解释:

  1. [a-zA-Z] - match any character from az or AZ (lower or capital case). [a-zA-Z] - 匹配 az 或 AZ 中的任何字符(小写或大写)。
  2. + - match one or more characters of what comes before + . + - 匹配+之前的一个或多个字符。

Your question misses some details.您的问题遗漏了一些细节。 Here is an answer making these assumptions:这是做出这些假设的答案:

  • you have a string with text that has <xyz> patterns, where xyz has 1+ alpha chars你有一个带有<xyz>模式的文本的字符串,其中xyz有 1+ alpha 字符
  • the string may have multiple <xyz> patterns该字符串可能有多个<xyz>模式
  • return value is an array of patterns found, empty if none返回值是找到的模式数组,如果没有则为空

Further, it is not clear if you need to extract the alpha chars with or without angle brackets, so here is an answer that shows both:此外,不清楚是否需要提取带或不带尖括号的 alpha 字符,因此这是一个同时显示两者的答案:

 const regex1 = /<[a-zA-Z]+>/g; const regex2 = /(?<=<)[a-zA-Z]+(?=>)/g; [ '<a>', '<hr>', 'foo <div> bar', 'foo <br> bar <br> moo', // matches '</nothing>', 'no tag' // no matches ].forEach(str => { let matches1 = str.match(regex1) || []; let matches2 = str.match(regex2) || []; console.log(str + ' => regex1: ' + JSON.stringify(matches1) + ' => regex2: ' + JSON.stringify(matches2)); });

Explanation of regex1: regex1的解释:

  • < -- angle bracket < -- 尖括号
  • [a-zA-Z]+ -- character class with 1+ alpha chars [a-zA-Z]+ -- 具有 1+ 个字母字符的字符类
  • > -- angle bracket > -- 尖括号

Explanation of regex2: regex2的解释:

  • (?<=<) -- positive lookbehind for an angle bracket (?<=<) -- 尖括号的正后视
  • [a-zA-Z]+ -- character class with 1+ alpha chars [a-zA-Z]+ -- 具有 1+ 个字母字符的字符类
  • (?=>) -- positive lookahead for an angle bracket (?=>) -- 尖括号的正向预测

Please note that not all browsers support lookbehind, notably Safari.请注意,并非所有浏览器都支持 lookbehind,尤其是 Safari。

Learn more about regex: https://twiki.org/cgi-bin/view/Codev/TWikiPresentation2018x10x14Regex了解有关正则表达式的更多信息: https ://twiki.org/cgi-bin/view/Codev/TWikiPresentation2018x10x14Regex

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

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