forked from AlexToro/regex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phpToJavascriptRegex.user.js
83 lines (79 loc) · 3.1 KB
/
phpToJavascriptRegex.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// ==UserScript==
// @name phpToJavascriptRegex
// @namespace phpToJavascriptRegex
// @version 1
// @grant none
// author: Alex Toro - [email protected]
/*
*
* @param {String} regex
* @returns {RegExp}
*
* This function try to transform a PHP regex to a JS one.
*/
function phpToJavascriptRegex(regex) {
var delimiter = regex.match(/^[^\w ]/);
if (delimiter) {
var modifiersRegex = '\\' + delimiter + '(.*)\\' + delimiter + '([a-zA-Z]*)$';
modifiersRegex = new RegExp(modifiersRegex, "i");
var regexParts = regex.match(modifiersRegex);
if (!regexParts)
return;
if (!regexParts[1])
return;
var regexContent = regexParts[1];
if (!checkEscapedDelimiters(regexContent, delimiter))
return;
var modifiers = 'g'; //This set the behavior as preg_match_all as default instead of a preg_match
if (regexParts[2]) {
var extraModifiers = regexParts[2];
for (var m = 0; m < extraModifiers.length; m++) {
if (extraModifiers[m] == 'U') { //Ungreedy
// The way of doing it in JS is to set ? after quantifiers. E.g: .* => .*?
regexContent = regexContent.replace(/([^\\])\+([^\?])/g, "$1+?$2");
regexContent = regexContent.replace(/([^\\])\+$/g, "$1+?");
regexContent = regexContent.replace(/([^\\])\*([^\?])/g, "$1*?$2");
regexContent = regexContent.replace(/([^\\])\*$/g, "$1*?");
regexContent = regexContent.replace(/([^\\])\}([^\?])/g, "$1}?$2");
regexContent = regexContent.replace(/([^\\])\}$/g, "$1}?");
}
else if (extraModifiers[m] == 's') { //Input as one single line
// The way of doing it is by replacing . for [\s\S]. Spaces also can be a new line.
regexContent = regexContent.replace(/^\.([\+\*\?])/g, "[\\s\\S]$2");
regexContent = regexContent.replace(/([^\\])\.([\+\*\?])/g, "$1[\\s\\S]$2");
regexContent = regexContent.replace(/ /g, "[\\s\\n]");
}
else if (extraModifiers[m] == 'i') { //Case Insensitive (like PHP regex)
modifiers = modifiers + extraModifiers[m];
}
}
}
return new RegExp(regexContent, modifiers);
}
else
return;
}
/*
*
* @param {String} regexContent
* @param {String} delimiter
* @returns {Boolean}
*
* Check if the regex has any not escaped delimiter inside.
* E.g: /hi/ how are/ it's not a valid regex, it has to be /hi\/ how are/
*/
function checkEscapedDelimiters(regexContent, delimiter) {
var escapedRegex = '[^\\\\]\\' + delimiter;
escapedRegex = new RegExp(escapedRegex, "i");
var match = regexContent.match(escapedRegex);
if (match) {
return false;
}
var escapedRegex = '^\\' + delimiter;
escapedRegex = new RegExp(escapedRegex, "i");
var match = regexContent.match(escapedRegex);
if (match) {
return false;
}
return true;
}