For example, if a website is pasted in the Zhihu editor, it is automatically converted to a link. The code found on the stack does not know how to use it. let's take a look. {Code...} is it replaced by monitoring keyup? Hope you can answer this question! Thank you! * ** Update: 2013-12-09: ***... for example, in the Zhihu editor, if you paste a website,
Is automatically converted to a link.
The code found on the stack does not know how to use it. let's take a look.
function replaceURLWithHTMLLinks(text) { var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; return text.replace(exp,"$1"); }
Is it replaced by monitoring keyup? Hope you can answer this question! Thank you!
* ** // Update: 2013-12-09 :***
The editor is
Reply content:
For example, if a website is pasted in the Zhihu editor,Is automatically converted to a link.
The code found on the stack does not know how to use it. let's take a look.
function replaceURLWithHTMLLinks(text) { var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; return text.replace(exp,"$1"); }
Is it replaced by monitoring keyup? Hope you can answer this question! Thank you!
* ** // Update: 2013-12-09 :***
The editor is
Not recommendedkeyupEvent, because it does not respond to non-keyboard operations, such as pasting the mouse into a Web site. UseonPropertyChangeEventtextareaChanges in the attribute values of the instance are generated.
In addition, we recommend that you add a space at the end of the regular expression, that isURL + spaceIt is resolved as a link. if it is a previous match, it will lead to an endless loop.
Http://jsfiddle.net/5kmqe/1/
linkify: function(inputText) { var replacedText, replacePattern1, replacePattern2, replacePattern3; var originalText = inputText; //URLs starting with http://, https://, file:// or ftp:// replacePattern1 = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gi; //URLs starting with "www." (without // before it, or it'd re-link the ones done above). replacePattern2 = /(^|[^\/f])(www\.[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gi; //Change email addresses to mailto:: links. replacePattern3 = /(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gi; //If there are hrefs in the original text, let's split // the text up and only work on the parts that don't have urls yet. var count = originalText.match(/ 0) { var combinedReplacedText; //Keep delimiter when splitting var splitInput = originalText.split(/(<\/a>)/g); for (i = 0; i < splitInput.length; i++) { if (splitInput[i].match(/$1').replace(replacePattern2, '$1$2').replace(replacePattern3, '$1'); } } combinedReplacedText = splitInput.join(''); return combinedReplacedText; } else { replacedText = inputText.replace(replacePattern1, '$1'); replacedText = replacedText.replace(replacePattern2, '$1$2'); replacedText = replacedText.replace(replacePattern3, '$1'); return replacedText; } },
For a piece of text, it can be run repeatedly.