Regular Expression - how do I loop through exec results - regex

In the following code the results are getting logged as expected.
I now want to loop through the individual items of the match in a loop but need help. match.split fails because of a null error TIA
var match;
var precedents=[];
while((match=exp.exec(text))) {
Logger.log(match[0]);
}
var matchArray=match.split();
if(matchArray.length>0){
for(i==0; i<matchArray.length;i++){
Logger.log(i);
precedents[i].push(matchArray[i]);
Logger.log(precedents[i]);
}
}

Related

Postman test never returns "no pass"

I'm trying make a test to my API server and I don't get test result no pass.
My code:
var data = JSON.parse(responseBody);
var days = data["days"];
var total_distance = 0;
days.forEach(function(value,index,array){
total_distance = total_distance + value["distance"];
});
pm.test("Distance data"),function(){
pm.expect(data["total_distance"].to.equal(total_distance));
}
This script never returns no pass. What is my error?
The syntax for your test is incorrect. You have a closing parentheses after pm.test("Distance data" which should actually be the last character on the last line.
Try:
pm.test("Distance data", function () {
pm.expect(data["total_distance"].to.equal(total_distance));
});

regex search term in variable

Do you know how I am able to put the search string inside the RegExp?
Let say if my search term is 'Ame', then I can write
v.name.search(new RegExp(/Ame/i)). //It works
var search = $("#search-query").val();
v.name.search(new RegExp('/'+search+'/i' //It doesn't work
However if the value 'Ame' was stored in the var 'search', how do I use the var?
var search = $("#search-query").val();
if($("#search-query").val().length <1){
$scope.hiddenError = true;
return;
}
$.each(json.products, function(i, v) {
if (v.name.search(new RegExp('/'+search+'/i')) != -1) { //doesn't work
$scope.recentGame.push({ label:v.name, value: v.type, link: v.url });
return;
}
});
When you want to build a custom regular expression from a string, you don't include the delimiters / or the options. You use the following form of the function constructor:
var regex = new RegExp(search, "i");
and use that in the search method.

Extract string using regex in stored procedure

I have a regex expression in javascript which works fine.
var re = /^([0-9]?[A-Z]+?)\s*(?:FM)?[FGHJKMNQUVXZ](?:[02]0)?[12]?[0-9]/i;
var str = 'RVBM2016';
var m;
if ((m = re.exec(str)) !== null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}
Now I am trying to reuse the same expression in my stored procedure in postgres. Here is how I am trying it:
select regexp_replace('BLM2016',
E'/^([0-9]?[A-Z]+?)\s*(?:FM)?[FGHJKMNQUVXZ](?:[02]0)?[12]?[0-9]/i', '', 'g')
This should return BL only. For RVBM2016 it should return RVB and so on..
But now it has no effect on the input text. Is there any syntactical mistake?
I was using the wrong method to extract string out of expression. The correct version is as follows if anyone is interested:
select (regexp_matches('BLM2016', '([0-9]?[A-Z]+?)\s*(?:FM)?[FGHJKMNQUVXZ](?:[02]0)?[12]?[0-9]', 'gi'))[1]

What is the equalient of JavaScript's "s.replace(/[^\w]+/g, '-')" in Dart language?

I am trying to get the following working code in JavaScript also working in Dart.
https://jsfiddle.net/8xyxy8jp/1/
var s = "We live, on the # planet earth";
var results = s.replace(/[^\w]+/g, '-');
document.getElementById("output").innerHTML = results;
Which gives the output
We-live-on-the-planet-earth
I have tried this Dart code
void main() {
print( "We live, on the # planet earth".replaceAll("[^\w]+","-"));
}
But the output becomes the same.
What am I missing here?
If you want replaceAll() to process the argument as regular expression you need to pass a RegExp instance. I usually use r as prefix for the regex string to make it a raw string where not interpolation ($, \, ...) takes place.
main() {
var s = "We live, on the # planet earth";
var result = s.replaceAll(new RegExp(r'[^\w]+'), '-');
print(result);
}
Try it in DartPad

How to check whether given email address is invalid in action script 3?

I need to check whether given email address is invalid in action script. Following is the code/regex i came up with.
private function isEmailInvalid(email:String):Boolean
{
var pattern:RegExp = /(\w|[_.\-])+#((\w|-)+\.)+\w{2,4}+/;
var result:Object = pattern.exec(email);
if(result == null) {
return true;
}
return false;
}
But it seems like above code do not cover all the test cases in the following link:
http://blogs.msdn.com/b/testing123/archive/2009/02/05/email-address-test-cases.aspx
Does anyone have better way of doing this?
Folowing are the tested valid emails i used (above function should return "false" for these):
firstname.lastname#domain.com
firstname+lastname#domain.com
email#domain.co.jp
Folowing are the invalid ones (so function should return "true" for these):
email#domain#domain.com
.email#domain.com
email..email#domain.com
plainaddress, email#domain..com
Remove the + at the last and you must need to put anchors.
^(\w|[_.\-])+#((\w|-)+\.)+\w{2,4}$
Simplified one,
^[\w_.-]+#([\w-]+\.)+\w{2,4}$
DEMO
Try this RegExp :
RegExp = /\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*/;