0

To convert semicolons (.,) into a certain link, or any particular text to a specific link. I hope you can understand me since I am using the translator.

For example this text is like this:

Hello world, how are you all?.

and I would like that by means of Javascript or CSS or with what you can to the text it will automatically generate a link to the semicolons. Example how it should look:

Hello world, how are you all?.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

0

You can try replacing every instance of a comma or period with an html <a> tag containing your url. This question explains a good format of replacing text in a webpage, shown below. In your case, you would want to use two commands (after defining a function similar to the one below). First, replaceTextOnPage('.', '<a>your link here</a>'); and second replaceTextOnPage(',', '<a>your link here</a>');. Thus, you would replace every comma (,) with one link, and every period (.) with another (or the same link, if you wish). I hope this helps!

function replaceTextOnPage(from, to){
  getAllTextNodes().forEach(function(node){
    node.nodeValue = node.nodeValue.replace(new RegExp(quote(from), 'g'), to);
  });

  function getAllTextNodes(){
    var result = [];

    (function scanSubTree(node){
      if(node.childNodes.length) 
        for(var i = 0; i < node.childNodes.length; i++) 
          scanSubTree(node.childNodes[i]);
      else if(node.nodeType == Node.TEXT_NODE) 
        result.push(node);
    })(document);

    return result;
  }

  function quote(str){
    return (str+'').replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
  }
}
Catogram
  • 303
  • 1
  • 12