JS to automatically convert text in parentheses with specific markup including match

后端 未结 3 864
情话喂你
情话喂你 2021-01-27 12:57

Upon page loading, I want to find all text in the body that is encapsulated in parenthess (here\'s an example) and replace it with the following:

<         


        
3条回答
  •  灰色年华
    2021-01-27 13:48

    I think this is what you're looking for. Use regex to find and replace "((" and "))". Given this content:

    without my having to actually DO anything special with the content? I could get in the habit of doubling up on the parenthesis if that helps ((like this would be a tooltip)) but (this would not).

    This code will search it and replace with your icon.

    var target = document.getElementById('content'),
        str = target.innerHTML,
        pre = '';
    str = str.replace(/\(\(/g, pre).replace(/\)\)/g, post);
    target.innerHTML = str;
    

    Here's a jsfiddle: https://jsfiddle.net/mckinleymedia/fzn4trmL/

    The title attribute doesn't have a lot of flexibility and this will have problems. You'd be much better off just surrounding the text by the icon, like so:

    like this would be a tooltip
    

    But then you need to have the tooltip show the html in the icon instead of the title.

提交回复
热议问题