我怎样在存在撇号的情况下给单引号之间的文本上色?

huangapple go评论52阅读模式
英文:

How can I color text between single quotes when apostrophes are present?

问题

我正在尝试给单引号中的短语上色。

我尝试过:

chapter = document.querySelector('#chapter');
chapter.innerHTML = chapter.innerHTML
  .replace(/'([^']*)'/g, "<span style='color:red'>$1</span>");

但正如您所看到的,我不知道如何避免像 "it's" 或 "Bob's house" 这样的情况。而且,由于某种原因,第一个 ' 被移除了。

英文:

I am trying to color phrases that are in single quotations.

I tried:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

chapter = document.querySelector(&#39;#chapter&#39;);
chapter.innerHTML = chapter.innerHTML
  .replace(/&#39;([^&#39;]*)&#39;/g, &quot;&lt;span style=&#39;color:red&#39;&gt;$1&lt;/span&quot;);

<!-- language: lang-html -->

&lt;div id=&quot;chapter&quot;&gt;It&#39;s Bob&#39;s house.&lt;/div&gt;

<!-- end snippet -->

But as you can see I don't know how to avoid cases like "it's" or "Bob's house". Also, for some reason the first ' gets removed.

答案1

得分: 2

你可以尝试将所有缩写中的单引号转换成插入符号的序列,执行高亮操作,最后再将插入符号恢复成单引号。

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
const chapter = document.querySelector('.chapter');

const highlightQuotes = (element, fg = 'inherit', bg = 'inherit') => {
  const style = `background:${bg};color:${fg}`;
  element.innerHTML = element.innerHTML
    .replace(/\b&#39;\b/g, '^^^')              // 缩写,例如 don&#39;t, he&#39;s
    .replace(/(?<!&#39;\w+)&#39;(?= )/g, '^^^')    // 所有格,例如 Luis&#39;
    .replace(/\^\^\^(\w+)\^\^\^/g, "'$1'") // 如果上述两者都运行了,修复
    .replace(/&#39;([^&#39;]+)&#39;/g,  `<span style="${style}">$1</span>`)
    .replace(/\^\^\^/g, "'");              // 修复原始的单引号
};

highlightQuotes(chapter, 'red', 'yellow');

<!-- language: lang-html -->
<div class="chapter">
  <p>'He's considered lucky with that curse?!'</p>
  <p>Almost like he was trying to 'bite at Su Yang's handsome face.'</p>
  <p>'I like Luis' shirt.'</p>
  <p>'abc' def</p>
  <p>Foo's' bar</p>
</div>

<!-- end snippet -->

正如Andy建议的那样,你也可以使用以下方式实现相同的效果:/&#39;(.*)&#39;/g

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
const chapter = document.querySelector('.chapter');

const highlightQuotes = (element, fg = 'inherit', bg = 'inherit') => {
  const style = `background:${bg};color:${fg}`;
  element.innerHTML = element.innerHTML
    .replace(/&#39;(.*)&#39;/g,  `<span style="${style}">$1</span>`)
};

highlightQuotes(chapter, 'red', 'yellow');

<!-- language: lang-html -->
<div class="chapter">
  <p>'He's considered lucky with that curse?!'</p>
  <p>Almost like he was trying to 'bite at Su Yang's handsome face.'</p>
  <p>'I like Luis' shirt.'</p>
  <p>'abc' def</p>
  <p>Foo's' bar</p>
</div>

<!-- end snippet -->
英文:

You could try converting all the single-quotes in contractions to a sequence of carets, perform the highlighting, and finally reverse the carets back into single-quotes.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const chapter = document.querySelector(&#39;.chapter&#39;);

const highlightQuotes = (element, fg = &#39;inherit&#39;, bg = &#39;inherit&#39;) =&gt; {
  const style = `background:${bg};color:${fg}`;
  element.innerHTML = element.innerHTML
    .replace(/\b&#39;\b/g, &#39;^^^&#39;)              // Contraction e.g. don&#39;t, he&#39;s
    .replace(/(?&lt;!&#39;\w+)&#39;(?= )/g, &#39;^^^&#39;)    // Possessive  e.g. Luis&#39;
    .replace(/\^\^\^(\w+)\^\^\^/g, &quot;&#39;$1&#39;&quot;) // If both above ran, repair
    .replace(/&#39;([^&#39;]+)&#39;/g,  `&lt;span style=&quot;${style}&quot;&gt;$1&lt;/span&gt;`)
    .replace(/\^\^\^/g, &quot;&#39;&quot;);              // Repair original apostrophes
};

highlightQuotes(chapter, &#39;red&#39;, &#39;yellow&#39;);

<!-- language: lang-html -->

&lt;div class=&quot;chapter&quot;&gt;
  &lt;p&gt;&#39;He&#39;s considered lucky with that curse?!&#39;&lt;/p&gt;
  &lt;p&gt;Almost like he was trying to &#39;bite at Su Yang&#39;s handsome face.&#39;&lt;/p&gt;
  &lt;p&gt;&#39;I like Luis&#39; shirt.&#39;&lt;/p&gt;
  &lt;p&gt;&#39;abc&#39; def&lt;/p&gt;
  &lt;p&gt;Foo&#39;s&#39; bar&lt;/p&gt;
&lt;/div&gt;

<!-- end snippet -->

As Andy suggests, you can achieve the same thing with: /&#39;(.*)&#39;/g

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const chapter = document.querySelector(&#39;.chapter&#39;);

const highlightQuotes = (element, fg = &#39;inherit&#39;, bg = &#39;inherit&#39;) =&gt; {
  const style = `background:${bg};color:${fg}`;
  element.innerHTML = element.innerHTML
    .replace(/&#39;(.*)&#39;/g,  `&lt;span style=&quot;${style}&quot;&gt;$1&lt;/span&gt;`)
};

highlightQuotes(chapter, &#39;red&#39;, &#39;yellow&#39;);

<!-- language: lang-html -->

&lt;div class=&quot;chapter&quot;&gt;
  &lt;p&gt;&#39;He&#39;s considered lucky with that curse?!&#39;&lt;/p&gt;
  &lt;p&gt;Almost like he was trying to &#39;bite at Su Yang&#39;s handsome face.&#39;&lt;/p&gt;
  &lt;p&gt;&#39;I like Luis&#39; shirt.&#39;&lt;/p&gt;
  &lt;p&gt;&#39;abc&#39; def&lt;/p&gt;
  &lt;p&gt;Foo&#39;s&#39; bar&lt;/p&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月14日 23:46:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76475357.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定