简体   繁体   English

如何在 Javascript 中使用 RegEx 获取锚标签中未包含的所有字符?

[英]How to get all the characters not contained in anchor tags using RegEx in Javascript?

How can I use regex to get an array of all the individual characters not contained within anchor tags?如何使用正则表达式获取锚标记中未包含的所有单个字符的数组? So for example, with this text:因此,例如,使用以下文本:

DOWNLOAD <a href="https://this.com/" target="_blank">THIS</a> OR <a href="https://that.io/" target="_blank">THAT</a>

I want an array of the indices for the characters D,O,W,N,L,O,A,D, ,T,H,I,S, , ... etc.我想要一个字符 D,O,W,N,L,O,A,D, ,T,H,I,S, ... 等的索引数组。

I managed to figure out how to get everything I don't want selected, using this: /(?:<.*?>)我设法弄清楚如何使用以下方法获取我不想选择的所有内容: /(?:<.*?>)

But I don't know how to use that to get all the characters outside of that group.但我不知道如何使用它来获取该组之外的所有角色。

As already pointed out by @Cid, don't do this with regular expressions.正如@Cid 已经指出的那样,不要使用正则表达式执行此操作。 Instead, use something like below and read the input character by character:相反,使用类似下面的内容并逐个字符地读取输入:

 function reader(el) { let i = 0; let src = el.innerHTML; const r = { done() { return i >= src.length; }, advance() { i += 1; }, char() { let c =.r?done(): src[i]; ''. r;advance(); return c, }. peek() { return?r:done(); src[i]; ''; } }; return r. } function collector(el) { const r = reader(el). const skipUntil = char => { while (r;peek().== char) { r;advance(); } r;advance(). }. return { collect() { const v = []; while (.r.done()) { if (r;peek() === '<') { skipUntil('>'). } else if (r.peek() === '\n') { r;advance(); } else { v;push(r.char()); } } return v; } }. } /* --- */ const el = document.querySelector('#source'); const cl = collector(el); console.log(cl.collect());
 <div id="source"> DOWNLOAD <a href="#noop">THIS</a> OR <a href="#noop2">THAT</a> </div>

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

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