Use PHP functions in JavaScript

JavaScript preg_quote

Quote regular expression characters plus an optional character

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
function preg_quote (str, delimiter) {
    // Quote regular expression characters plus an optional character  
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/preg_quote    // +   original by: booeyOH
    // +   improved by: Ates Goral (http://magnetiq.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +   improved by: Brett Zamir (http://brett-zamir.me)    // *     example 1: preg_quote("$40");
    // *     returns 1: '\$40'
    // *     example 2: preg_quote("*RRRING* Hello?");
    // *     returns 2: '\*RRRING\* Hello\?'
    // *     example 3: preg_quote("\\.+*?[^]$(){}=!<>|:");    // *     returns 3: '\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:'
    return (str+'').replace(new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\'+(delimiter || '')+'-]', 'g'), '\\$&');
}
external links: original PHP docs | raw js source

Examples

» Example 1

Running

1
preg_quote("$40");

Should return

1
'\$40'

» Example 2

Running

1
preg_quote("*RRRING* Hello?");

Should return

1
'\*RRRING\* Hello\?'

Dependencies

No dependencies, you can use this function standalone.

Open syntax issues

php.js uses JsLint to help us keep our code consistent and prevent some common bugs.

Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.


Authors

Thanks to the following developers, you get to have preg_quote goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Kevin van Zonneveld
13 Apr '08 Permalink

q  @ ?: Hi, thanks for the contribution. There are some things that keep me from including this in php.js. Firstly I don't know your name so I cannot credit you, Secondly, I don't know if the perl regular expressions are 100% compatible with the javascript regex engine. Do you know this?

Gravatar
Simple preg_replace_callback
12 Apr '08 Permalink

q  [CODE=&quot;Javascript&quot;]
function preg_replace_callback(reg, cbck, str){
return str.replace(REGEXP, function(arguments){return window[cbck](arguments);});
}
[/CODE]

Gravatar
Kevin van Zonneveld
23 Jan '08 Permalink

q  @ Ates Goral: Kaizen principles FTW

Gravatar
Ates Goral
23 Jan '08 Permalink

q  After looking at the implementation of addslashes(), I realized that this could have been simply achieved with:

[CODE=&quot;Javascript&quot;]
return str.replace(/([\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\&lt;\&gt;\|\:])/g, &quot;\\$1&quot;);
[/CODE]

I don't know why I chose to use a replacement function in the first place :)

Gravatar
Kevin van Zonneveld
23 Jan '08 Permalink

q  @ Ates Goral: There you go, 2 gold medals :)

Gravatar
Ates Goral
23 Jan '08 Permalink

q  Here's my take:

[CODE=&quot;Javascript&quot;]
function preg_quote(str) {
// * example 1: preg_quote(&quot;*RRRING* Hello?&quot;);
// * returns 1: &quot;\\*RRRING\\* Hello\\?&quot;
// * example 2: preg_quote(&quot;\\.+*?[^]$(){}=!&lt;&gt;|:&quot;);
// * returns 2: &quot;\\\\\\.\\+\\*\\?\\[\\^\\]\\$\\(\\)\\{\\}\\=\\!\\&lt;\\&gt;\\|\\:&quot;

return str.replace(/[\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\&lt;\&gt;\|\:]/g, function(c) { return &quot;\\&quot; + c; });
}
[/CODE]


Contribute a New function

More functions

In this category

preg_grep
» preg_quote
sql_regcase

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: @HalfWinter, Paulo Freitas, Andros Peña Romo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>

Click here to lend your support to: phpjs and make a donation at www.pledgie.com !