JavaScript tutorial:
source property

 

Applies To: Regular Expression Object

The source property returns a copy of the text of the regular expression pattern. Read-only.

Syntax

rgexp.source

The rgexp argument is a Regular expression object. It can be a variable name or a literal.

Return value

Returns a copy of the text of the regular expression pattern.

The following example illustrates the use of the source property:

function SourceDemo(re, s)
{
    var s1;
    // Test string for existence of regular expression.
    if (re.test(s))
        s1 = " contains ";
    else
        s1 = " does not contain ";
    // Get the text of the regular expression itself.
    return(s + s1 + re.source);
}
document.write(SourceDemo(new RegExp("Was"),"Washington"));

To run the code, paste it into JavaScript Editor, and click the Execute button.

See Also: Regular Expression Object Methods, Regular Expression Object Properties, Regular Expression Syntax