Created
November 3, 2015 10:32
-
-
Save kidatti/cb1ed7ff102ede0a53df to your computer and use it in GitHub Desktop.
Replace String to Link for JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
</head> | |
<body> | |
<script> | |
var source = '2つあったらhttps://www.google.co.jp/%xxxどうなるのhttp://www.yahoo.co.jp/'; | |
var result = replace(source); | |
console.log(result); | |
function replace(source) { | |
var result = source.replace(/(http(s)?:\/\/[\x21-\x7e]+)/gi, "<a href='$1' target='_blank'>$1</a>"); | |
return result; | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment