بنحكي في هذا الدرس عن regular expression الي هو عبارة عن سلسلة من الأحرف التي تشكل نمط بحث(search pattern) داخل النصوص بحيث يمكن التحكم بعملية البحث، وتحديد طريقة البحث داخل النصوص، كما تستخدم أيضا في عمليات استبدال النصوص text replace operations. يدعم JavaScript مجموعه من أنماط البحث (search pattern) ويمكن استخدام حرف واحد single character او اكثر من ذلك (نمط معقد complicated pattern)
const re = /ab+c/;
Const re =/Devkum/g
<!DOCTYPE html>
<html>
<body onunload="">
<h3>JavaScript Templates Literals</h3>
<p id="Date"></p>
<script>
let text = "Welcome to Devkum, A community of thousands of aspiring people!";
let n = text.search("Devkum");
document.write( n);
document.write("<br>");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body onunload="">
<h3>JavaScript Templates Literals</h3>
<p id="Date"></p>
<script>
let text = "Welcome to Devkum, A community of thousands of aspiring people!";
let n = text.search("devkum");
document.write( n);
document.write("<br>");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body onunload="">
<h3>JavaScript</h3>
<p id="Date"></p>
<script>
let text = "Welcome to Devkum, A community of thousands of aspiring people!";
let n = text.search(/devkum/i);
document.write( n);
document.write("<br>");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body onunload="">
<h3>JavaScript</h3>
<p id="Date"></p>
<script>
let text = "Welcome to Devkum, A community of thousands of aspiring people!";
let n = text.search(/devkum/g);
document.write( n);
document.write("<br>");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body onunload="">
<h3>JavaScript</h3>
<p id="Date"></p>
<script>
let text = "Welcome to WebSite, A community of thousands of aspiring people!";
let n = text.replace("WebSite","Devkum");
document.write( n);
document.write("<br>");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body onunload="">
<h3>JavaScript</h3>
<p id="Date"></p>
<script>
let text = "Welcome to WebSite, A community of thousands of aspiring people!";
let n = text.replace(/webSite/i,"Devkum");
document.write( n);
document.write("<br>");
</script>
</body>
</html>
العلامة flag | الوصف Description |
d | إنشاء مؤشرات لمطابقات السلاسل الفرعية. |
g | بحث global مع Case-sensitive، يتم البحث عن جميع الكلمات المتطابقه |
i | بحث Case-insensitive |
m | بحث متعدد الاسطر Multi-line search. |
s | يسمح باستخدام . لتتناسب مع أحرف السطر الجديد. match newline characters |
u | "unicode" تعامل مع pattern على أنه سلسلة من unicode code points. |
y | يستخدم لإجراء بحث ثابت (sticky) بحيث يطابق من بداية الموضع الحالي في السلسلة |
<!DOCTYPE html>
<html>
<body onunload="">
<h3>JavaScript</h3>
<p id="Date"></p>
<script>
let text = "Welcome to WebSite, A community of thousands of aspiring people!";
let n = text.match(/[t]/g);
document.write( n);
document.write("<br>");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body onunload="">
<h3>JavaScript</h3>
<p id="Date"></p>
<script>
let text = "Welcome to WebSite, A community of thousands of aspiring people!";
let n = text.match(/[t-x]/g);
document.write( n);
document.write("<br>");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body onunload="">
<h3>JavaScript</h3>
<p id="Date"></p>
<script>
let text = "123456789";
let result = text.match(/[1-4]/g);
document.write( result);
document.write("<br>");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body onunload="">
<h3>JavaScript</h3>
<p id="Date"></p>
<script>
let text = "Current Temperature is 35 C";
let result = text.match(/\d/g);
document.write( result);
document.write("<br>");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body onunload="">
<h3>JavaScript</h3>
<p id="Date"></p>
<script>
let text = "Current Temperature is 35 C";
let result = text.match(/\s/g);
document.write( result);
document.write("<br>");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body onunload="">
<h3>JavaScript</h3>
<p id="Date"></p>
<script>
let text = "HELLO, LOOK AT YOU!";
let result = text.search(/\bLO/);
document.write( result);
document.write("<br>");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body onunload="">
<h3>JavaScript</h3>
<p id="Date"></p>
<script>
let text = "HELLO, LOOK AT YOU!";
let result = text.search(/LO\b/);
document.write( result);
document.write("<br>");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body onunload="">
<h3>JavaScript</h3>
<p id="Date"></p>
<script>
let text = "HELLOOOO, LOOK AT YOU!";
let result = text.search(/LO\b/);
document.write( result);
document.write("<br>");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body onunload="">
<h3>JavaScript</h3>
<p id="Date"></p>
<script>
let text = "Devkum Hello World!";
let result = text.match(/\u0057/g);
document.write( result);
document.write("<br>");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body onunload="">
<h3>JavaScript</h3>
<p id="Date"></p>
<script>
let text = "Devkum We made it easy.";
let result = text.match(/e+/g);
document.write( result);
document.write("<br>");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body onunload="">
<h3>JavaScript</h3>
<p id="Date"></p>
<script>
let text = "Devkum We made it eassssy.";
let result = text.match(/ea*/g);
document.write( result);
document.write("<br>");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body onunload="">
<h3>JavaScript</h3>
<p id="Date"></p>
<script>
let text = "Devkum We made it eassssy.";
let result = text.match(/ea?/g);
document.write( result);
document.write("<br>");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body onunload="">
<h3>JavaScript</h3>
<p id="Date"></p>
<script>
let text = "Devkum We made it eassssy.";
const pattern = /e/;
document.write( pattern.test(text));
document.write("<br>");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body onunload="">
<h3>JavaScript</h3>
<p id="Date"></p>
<script>
let text = "Devkum We made it eassssy.";
document.write( /e/.test(text));
document.write("<br>");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body onunload="">
<h3>JavaScript</h3>
<p id="Date"></p>
<script>
let pattern = /Hello World/g;
let text = pattern.toString();
document.write(text);
document.write("<br>");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body onunload="">
<h3>JavaScript</h3>
<p id="Date"></p>
<script>
let text = /Hello World/g.toString();
document.write(text);
document.write("<br>");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body onunload="">
<h3>JavaScript</h3>
<p id="Date"></p>
<script>
const obj = /e/.exec("Devkum We made it eassssy.");
document.write("Found " + obj[0] + " in position " + obj.index + " in the text: " + obj.input);
document.write("<br>");
</script>
</body>
</html>