https://cdn.statically.io/gh/dte-project/a/1e23173799f8ac7c346f345f20de4e5ccb246b1e/shell.v2.min.css
Loading...

Postingan Terbaru

Judul artikel
Tampilkan postingan dengan label Regex. Tampilkan semua postingan
Tampilkan postingan dengan label Regex. Tampilkan semua postingan

Rabu, 06 Juli 2016

Ekstraksi Umum

Memisahkan antara tag HTML dengan teks di sekitarnya:

var test = 'foo <a b="c">bar</a> baz <b> qux yo"s <c><d> wut?';
var results = [];
test.replace(/([^<>]*?)(<\/?[-:\w]+(?:>|\s[^<>]*?>)|$)/g, function(a, b, c) {
if (b) results.push(b);
if (c) results.push(c);
});

console.log(results);

Hasil:

[
'foo ',
'<a b="c">',
'bar',
'</a>',
' baz ',
'<b>',
' qux yo"s ',
'<c>',
'<d>',
' wut?'
]

Dengan cara ini Anda dapat melakukan manipulasi teks dengan aman tanpa harus mempengaruhi tag HTML. Caranya adalah dengan mengecek terlebih dahulu apakah anggota merupakan tag HTML atau teks:

var output = "";
for (var i = 0, len = results.length; i < len; ++i) {
var s = results[i];
// Is this a HTML tag or text?
if (s && s[0] === '<' && s.slice(-1) === '>') {
// this is a HTML tag …
output += s;
} else {
// do something with text …
output += s.replace(/"/g, '&quot;');
}
}

console.log(output);

Hasil:

'foo <a b="c">bar</a> baz <b> qux yo&quot;s <c><d> wut?'

Ekstraksi Spesifik

Kode di bawah ini akan memisahkan antara tag HTML dengan nama tertentu saja, sehingga teks bisa saja berisi tag HTML yang diperbolehkan. Ini berguna jika Anda ingin melakukan manipulasi teks di luar tag tertentu saja, misalnya tag <kbd>, <pre>, <script> dan <style>:

var test = 'foo <a b="c">bar</a> baz <b> qux yo"s <c><d> wut? <kbd>ignore this yo"s</kbd>';
var results = [];
test.replace(/(.*?)(<\/?(?:kbd|pre|script|style)(?:>|\s[^<>]*?>)|$)/g, function(a, b, c) {
if (b) results.push(b);
if (c) results.push(c);
});

console.log(results);

Hasil:

[
'foo <a b="c">bar</a> baz <b> qux yo"s <c><d> wut? ',
'<kbd>',
'ignore this yo"s',
'</kbd>'
]

Berikut ini adalah cara mengubah karakter " yang berada di luar tag <kbd> menjadi &quot;:

var output = "";
var skip = false;
for (var i = 0, len = results.length; i < len; ++i) {
var s = results[i];
if (s && s[0] === '<' && s.slice(-1) === '>') {
// check whether the HTML tag is an opening or closing tag …
skip = s[1] && s[1] !== '/';
output += s;
} else {
output += !skip ? s.replace(/"/g, '&quot;') : s; // replace or skip …
}
}

console.log(output);

Hasil:

'foo <a b=&quot;c&quot;>bar</a> baz <b> qux yo&quot;s <c><d> wut? <kbd>ignore this yo"s</kbd>'

Versi PHP

PHP sudah memiliki fungsi khusus untuk menangani tugas ini:

$test = 'foo <a b="c">bar</a> baz <b> qux yo"s <c><d> wut?';
$results = preg_split('#(<\/?[-:\w]+(?:>|\s[^<>]*?>))#', $test, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);

var_dump($results);

Untuk melakukan ekstraksi tag HTML secara spesifik:

$results = preg_split('#(<\/?(?:kbd|pre|script|style)(?:>|\s[^<>]*?>))#', $test, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);

Jumat, 05 Februari 2016

Dukungan Markup

Mendukung hampir semua markup seperti yang dinyatakan dalam dokumentasi Markdown Extra, kecuali markup catatan kaki, abbreviasi, daftar definisi, tautan ber–referensi dan gambar ber–referensi.

Jumat, 28 Februari 2014

Versi PHP

function get_first_image_url($data, $default_url = null) {

/**
* Matched with ...
* ----------------
*
* [1]. `![alt text](IMAGE URL)`
* [2]. `![alt text](IMAGE URL "optional title")`
*
* ... and the single-quoted version of them
*
*/
if(preg_match_all('#\!\[.*?\]\(([^\s]+?)( +([\'"]).*?\3)?\)#', $data, $matches)) {
return $matches[1][0];
}

/**
* Matched with ...
* ----------------
*
* [1]. `<img src="IMAGE URL">`
* [2]. `<img foo="bar" src="IMAGE URL">`
* [3]. `<img src="IMAGE URL" foo="bar">`
* [4]. `<img src="IMAGE URL"/>`
* [5]. `<img foo="bar" src="IMAGE URL"/>`
* [6]. `<img src="IMAGE URL" foo="bar"/>`
* [7]. `<img src="IMAGE URL" />`
* [8]. `<img foo="bar" src="IMAGE URL" />`
* [9]. `<img src="IMAGE URL" foo="bar" />`
*
* ... and the uppercased version of them, and the single-quoted version of them
*
*/
if(preg_match_all('#<img .*?src=([\'"])([^\'"]+?)\1.*? *\/?>#i', $data, $matches)) {
return $matches[2][0];
}

return $default_url; // Default image URL if nothing matched
}

Penggunaan

<img src="<?php echo get_first_image_url($page['content'], 'img/no-image.png'); ?>">

$page['content'] cuma contoh. Anda harus mengambilnya dari konten posting Anda sesuai dengan API pada CMS yang Anda pakai.

Versi JavaScript

function getFirstImageURL(data, noImage) {

/**
* Matched with ...
* ----------------
*
* [1]. `![alt text](IMAGE URL)`
* [2]. `![alt text](IMAGE URL "optional title")`
*
* ... and the single-quoted version of them
*
*/
var matches = /\!\[.*?\]\(([^\s]+?)( +[\'"].*?[\'"])?\)/.exec(data);
return matches ? matches[1] : noImage;

/**
* Matched with ...
* ----------------
*
* [1]. `<img src="IMAGE URL">`
* [2]. `<img foo="bar" src="IMAGE URL">`
* [3]. `<img src="IMAGE URL" foo="bar">`
* [4]. `<img src="IMAGE URL"/>`
* [5]. `<img foo="bar" src="IMAGE URL"/>`
* [6]. `<img src="IMAGE URL" foo="bar"/>`
* [7]. `<img src="IMAGE URL" />`
* [8]. `<img foo="bar" src="IMAGE URL" />`
* [9]. `<img src="IMAGE URL" foo="bar" />`
*
* ... and the uppercased version of them, and the single-quoted version of them
*
*/
matches = /<img .*?src=[\'"]([^\'"]+?)[\'"].*? *\/?>/i.exec(data);
return matches ? matches[1] : noImage;

}

Penggunaan

var img = '<img src="' + getFirstImageURL(sourceText, 'img/no-image.png') + '">';
document.write(img);

sourceText cuma contoh. Anda harus mengambilnya dari konten posting Anda, misalnya dengan cara ini:

var sourceText = document.querySelector('.post-body').innerHTML;

Kamis, 13 Februari 2014

<("[^"]*"|'[^']*'|[^'">])*>

Cocok untuk pola ini:

  1. <input value='>'>
  2. <input value='<'>
  3. <a href="http://www.nama_web.com">
  4. <br>, <br/>
  5. <input value="'" id="test">, <input value='"' id='test'>

Rabu, 19 Desember 2012

Fungsi ini digunakan untuk mengubah deret angka menjadi pola mata uang dimana setiap tiga deret angka akan ditambahi sebuah titik atau koma setelahnya/sebelumnya, dimulai dari belakang:

function addCommas(nStr) {
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}

Penggunaan

addCommas('10000000'); // Hasil => 10,000,000

Demo

View more articles

Keyword link

Subscribe via Email

Sign up by email to get the latest news from us..

Contact us

Send a message to us.

Template Hasil Cloning II | Copyright 2015 - 2016 | Rip Code by Shn | ShannenPio Cloning