This repository has been archived by the owner on Feb 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathregex.js
59 lines (57 loc) · 1.96 KB
/
regex.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
/*******************************************************************************
* @license
* Copyright (c) 2011, 2013 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License v1.0
* (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution
* License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html).
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
/*eslint-env browser, amd*/
/**
* @name orion.regex
* @class Utilities for dealing with regular expressions.
* @description Utilities for dealing with regular expressions.
*/
define("orion/regex", [], function() { //$NON-NLS-0$
/**
* @memberOf orion.regex
* @function
* @static
* @description Escapes regex special characters in the input string.
* @param {String} str The string to escape.
* @returns {String} A copy of <code>str</code> with regex special characters escaped.
*/
function escape(str) {
return str.replace(/([\\$\^*\/+?\.\(\)|{}\[\]])/g, "\\$&"); //$NON-NLS-0$
//return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}
/**
* @memberOf orion.regex
* @function
* @static
* @description Parses a pattern and flags out of a regex literal string.
* @param {String} str The string to parse. Should look something like <code>"/ab+c/"</code> or <code>"/ab+c/i"</code>.
* @returns {Object} If <code>str</code> looks like a regex literal, returns an object with properties
* <code><dl>
* <dt>pattern</dt><dd>{String}</dd>
* <dt>flags</dt><dd>{String}</dd>
* </dl></code> otherwise returns <code>null</code>.
*/
function parse(str) {
var regexp = /^\s*\/(.+)\/([gim]{0,3})\s*$/.exec(str);
if (regexp) {
return {
pattern : regexp[1],
flags : regexp[2]
};
}
return null;
}
return {
escape: escape,
parse: parse
};
});