Entry
JavaScript: Regular expression: Simple: Create: How create regular expression in JavaScript?
Dec 8th, 2004 07:48
Knud van Eeden,
----------------------------------------------------------------------
--- Knud van Eeden --- 08 December 2004 - 05:00 pm -------------------
JavaScript: Regular expression: Simple: Create: How create regular
expression in JavaScript?
---
General format (when using one liners e.g. to test in an 'alert'
statement):
--- cut here: begin --------------------------------------------------
<!-------------------------------------------------------------------->
<HTML>
<!-------------------------------------------------------------------->
<SCRIPT>
alert( /<your regular expression>/<your parameters>.test( <your
string> ) ); // gives true or false
</SCRIPT>
<!-------------------------------------------------------------------->
</HTML>
<!-------------------------------------------------------------------->
---
---
e.g. test if a string equals a 0.
---
--- cut here: begin --------------------------------------------------
<!-------------------------------------------------------------------->
<HTML>
<!-------------------------------------------------------------------->
<SCRIPT>
alert( /0/.test( "0" ) ); // gives true
</SCRIPT>
<!-------------------------------------------------------------------->
</HTML>
<!-------------------------------------------------------------------->
--- cut here: end ----------------------------------------------------
---
---
e.g. test if a string contains an 'a'.
---
--- cut here: begin --------------------------------------------------
<!-------------------------------------------------------------------->
<HTML>
<!-------------------------------------------------------------------->
<SCRIPT>
alert( /.*a.*/.test( "a" ) ); // gives true
</SCRIPT>
<!-------------------------------------------------------------------->
</HTML>
<!-------------------------------------------------------------------->
--- cut here: end ----------------------------------------------------
---
e.g. test if a string contains an 'a'.
---
--- cut here: begin --------------------------------------------------
<!-------------------------------------------------------------------->
<HTML>
<!-------------------------------------------------------------------->
<SCRIPT>
alert( /.*a.*/.test( "eeeeeeaeeeeeee" ) ); // gives true
</SCRIPT>
<!-------------------------------------------------------------------->
</HTML>
<!-------------------------------------------------------------------->
--- cut here: end ----------------------------------------------------
---
e.g. test if a string contains an 'a'.
---
--- cut here: begin --------------------------------------------------
<!-------------------------------------------------------------------->
<HTML>
<!-------------------------------------------------------------------->
<SCRIPT>
alert( /.*a.*/.test( "eeeeeeeeeeeee" ) ); // gives false
</SCRIPT>
<!-------------------------------------------------------------------->
</HTML>
<!-------------------------------------------------------------------->
--- cut here: end ----------------------------------------------------
---
---
---
e.g. test if a string contains 'Knud'.
---
--- cut here: begin --------------------------------------------------
<!-------------------------------------------------------------------->
<HTML>
<!-------------------------------------------------------------------->
<SCRIPT>
alert( /.*Knud.*/.test( "Hello from Knud today" ) ); // gives true
</SCRIPT>
<!-------------------------------------------------------------------->
</HTML>
<!-------------------------------------------------------------------->
---
---
e.g. test if a string contains 'knud' (case insensitive).
You use thus the 'i' (or 'i'gnore case) parameter.
You can attach the parameters at the end of your regular expression.
---
<!-------------------------------------------------------------------->
<HTML>
<!-------------------------------------------------------------------->
<SCRIPT>
alert( /.*Knud.*/i.test( "Hello from knud today" ) ); // gives true
</SCRIPT>
<!-------------------------------------------------------------------->
</HTML>
<!-------------------------------------------------------------------->
--- cut here: end ----------------------------------------------------
----------------------------------------------------------------------