Skip to content

Instantly share code, notes, and snippets.

@waskito
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waskito/8932191 to your computer and use it in GitHub Desktop.
Save waskito/8932191 to your computer and use it in GitHub Desktop.
Belajar Javascript Regular Expression http://plnkr.co/edit/iVsTSFhHABQ3rKVeBSrp
<!DOCTYPE html>
<html>
<head>
<title>Try Javascript Regex</title>
</head>
<body>
<h1>Hello Regex</h1>
<p id="text">[file]http://www.startupjobs.asia/wp-content/uploads/company_logos/2013/10/qiscus-logo1.png[/file] Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. http://tech.qisc.us </p>
Regex Result:
<div id="rgx"></div>
<script src="script.js"></script>
</body>
</html>
var text = document.getElementById('text').innerHTML;
var rgx = document.getElementById('rgx');
var regex1 = /\[file\].*\[\/file\]/;
if(text.match(regex1)){
var match = text.match(regex1);
match = match[0].substring(6).slice(0,-7);
match = "<img src='"+match+"'>"
document.getElementById('text').innerHTML = text.replace(regex1,match);
}
rgx.innerHTML = match;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment