This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 4 years ago.
I'm trying to match some paragraphs in Google Docs but the pattern that I wanted to use for it doesn't match the string when run inside a Google Script. However, it works properly on regex101 so I guess I'm missing something. Do you know what?
This is a sample of what I have:
function test() {
var str = "brown fox → jumps over the lazy dog";
var definitionRe = new RegExp('([\w\s]+)\s+[\u2192]\s+(.+)', 'g');
var definitionMatch = definitionRe.exec(str); // null
var dummy = "asdf"; // makes the debugger happy to break here
}
When using a string regex such as new RegExp(...), you need to escape your \'s, so then the following:
var definitionRe = new RegExp('([\w\s]+)\s+[\u2192]\s+(.+)', 'g');
Will become an escaped version like this:
var definitionRe = new RegExp('([\\w\\s]+)\\s+[\\u2192]\\s+(.+)', 'g');
Otherwise you can do a non string version, but you then can no longer concatenate values to the string (If that is something you would like):
var definitionRe = /([\w\s]+)\s+[\u2192]\s+(.+)/g;
Related
This question already has answers here:
Difference between matches() and find() in Java Regex
(5 answers)
Closed 3 years ago.
Same regex, different results;
Java
String regex = "Windows(?=95|98|NT|2000)";
String str = "Windows2000";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
System.out.println(m.matches()); // print false
JavaScript
var value = "Windows2000";
var reg = /Windows(?=95|98|NT|2000)/;
console.info(reg.test(value)); // print true
I can't understand why this is the case?
From the documentation for Java's Matcher#matches() method:
Attempts to match the entire region against the pattern.
The matcher API is trying to apply your pattern against the entire input. This fails, because the RHS portion is a zero width positive lookahead. So, it can match Windows, but the 2000 portion is not matched.
A better version of your Java code, to show that it isn't really "broken," would be this:
String regex = "Windows(?=95|98|NT|2000)";
String str = "Windows2000";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
while (m.find()) {
System.out.println(m.group()); // prints "Windows"
}
Now we see Windows being printed, which is the actual content which was matched.
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 4 years ago.
I'm looking for some assistance with JavaScript/Regex when trying to format a string of text.
I have the following IDs:
00A1234/A12
0A1234/A12
A1234/A12
000A1234/A12
I'm looking for a way that I can trim all of these down to 1234/A12. In essence, it should find the first letter from the left, and remove it and any preceding numbers so the final format should be 0000/A00 or 0000/AA00.
Is there an efficient way this can be acheived by Javascript? I'm looking at Regex at the moment.
Instead of focussing on what you want to strip, look at what you want to get:
/\d{4}\/[A-Z]{1,2}\d{2}/
var str = 'fdfhfjkqhfjAZEA0123/A45GHJqffhdlh';
match = str.match(/\d{4}\/[A-Z]{1,2}\d{2}/);
if (match) console.log(match[0]);
You could seach for leading digits and a following letter.
var data = ['00A1234/A12', '0A1234/A12', 'A1234/A12', '000A1234/A12'],
regex = /^\d*[a-z]/gi;
data.forEach(s => console.log(s.replace(regex, '')));
Or you could use String#slice for the last 8 characters.
var data = ['00A1234/A12', '0A1234/A12', 'A1234/A12', '000A1234/A12'];
data.forEach(s => console.log(s.slice(-8)));
You could use this function. Using regex find the first letter, then make a substring starting after that index.
function getCode(s){
var firstChar = s.match('[a-zA-Z]');
return s.substr(s.indexOf(firstChar)+1)
}
getCode("00A1234/A12");
getCode("0A1234/A12");
getCode("A1234/A12");
getCode("000A1234/A12");
A regex such as this will capture all of your examples, with a numbered capture group for the bit you're interested in
[0-9]*[A-Z]([0-9]{4}/[A-Z]{1,2}[0-9]{2})
var input = ["00A1234/A12","0A1234/A12","A1234/A12","000A1234/A12"];
var re = new RegExp("[0-9]*[A-Z]([0-9]{4}/[A-Z]{1,2}[0-9]{2})");
input.forEach(function(x){
console.log(re.exec(x)[1])
});
This question already has answers here:
JavaScript RegExp objects
(5 answers)
Closed 6 years ago.
I am trying to validate if a string I entered matches the date format 'MM/yyyy'
Below is a sample of the code I am using for the same:
var date='05/2016'
var patt= new RegExp('^((0[1-9])|(1[0-2])|[1-9])\/(\d{4})$');
patt.test(date);
However the above code is returning false.
I tried running it with the regex checker:
https://regex101.com/
The pattern seems to be working fine.
Could someone please let me know what is missing.
https://jsfiddle.net/ymj6o8La/
You have to escape the string that is passed to RegExp (the backslashes).
var patt= new RegExp('^((0[1-9])|(1[0-2])|[1-9])\\/(\\d{4})$');
Even better, in your case, it's not dynamic, so you should use the literal RegExp instead
var patt = /^((0[1-9])|(1[0-2])|[1-9])\/(\d{4})$/
You should escape your backslashes. To represent \d or even \ you should another backslash behind it (e.g: \\) :
var date = '05/2016'
var patt = new RegExp('^((0[1-9])|(1[0-2])|[1-9])\\/(\\d{4})$');
console.log(patt.test(date));
Try using a pattern like this
patt= /^((0[1-9])|(1[0-2]))\/(\d{4})$/;
This question already has answers here:
How to replace multiple keywords by corresponding keywords with the `replace` method?
(3 answers)
Closed 7 years ago.
This is a chunk of Google Apps Script code:
var re = /(<.*?>)+/;
var strip = str.replace(re, "");
Logger.log(strip);
Why does it strip only the first instance of tag?
var re = /(<.*?>)/g
The trailing g is a flag you need to set to replace all matching instances. Depending on the content of str you are passing Another flag you may wish to try adding is m which signifies that the pattern should apply to multiple lines i.e.
var re = /(<.*?>)/mg
This question already has answers here:
Converting user input string to regular expression
(14 answers)
Closed 9 years ago.
I am using a very fine JavaScript library called "array-query" by Jacob Wright to do searches in arrays of objects.
One method is regex() where a regular expression can be included in parentheses like this: regex(/[^\w\s]/). If I hardcode the expression as I just showed it works fine. If I put the same expression in a variable first it does not work, like this:
var reg = "/[^\w\s]/";
regex(reg);
I was told
You are putting quotes around your regex, making it a string. Remove the quotes.
Thus
var reg = /[^\w\s]/;
regex(reg);
works fine.
Problem is I need to accept the user input from an textbox as part of the regular expression. For example if the user types in the letter z it needs to get changed to /z/. Even if I type in /z/ the textbox.value returned has the same problem as a var reg = "/z/". If I hardcode var reg = /z/; regex(reg); it works fine.
How to make a input textbox value of "z" into a form that is var reg = z;?
Many many thanks for any help or ideas, hope this isn't too confusing.
You should do
var regex = new RegExp('your regex string');
Basically you can think of
var regex = /pattern/modifiers;
as
var regex = new RegExp(pattern,modifiers);
Read more about it at: MDN or w3schools
var reg = new RegExp("string");
You can do something like this:
var string = $('#input_id').val();
string = string.replace('/', '');
var regexpPattern = '/'+string+'/';
regex(regexpPattern);