Pass regex to variable (angular) - angularjs

I have such a code ng-init="validationRegex = '#RegularExpression.expression'" where RegularExpression.expression is c# string variable = "(\w+\/|\w+\\)+(\w+)\.\w+". I want to pass variable value to angular controller using ng-init. But in the end I get (w+/|w+\)+(w+).w+. How can I get right value?

I assume that your C# code is actually: #"(\w+\/|\w+\\)+(\w+)\.\w+". The # avoids needing the evil escaped escape.
You will likely need to use the evil escaped escape in this case. Note that you don't need to escape the / if you are putting it directly in a string (if you are using it how I think you are). You can also use braces to minimize the escaping. So you can take this regex:
(\w+/|\w+\\)+(\w+)[.]\w+
and pass it through regex planet to get this:
"(\\w+/|\\w+\\\\)+(\\w+)[.]\\w+"
I would not recommend manual conversion since you already have an escaped backslash.

Double escape the string using:
#RegularExpression.expression.Replace(#"\", #"\\")
When it comes through on the JavaScript side it will be singly-escaped again.

Related

regex look-behind expression incompatibily with IE

I am using this regex expression for an input. When I try it in Chrome, it works well, but not when I try in IE. The regex editor that I am using advice me that the negative look-behind expression could not work for some browsers.
How can I adapt the expression to make it work for IE? I am using it to make impossible to end the input with /.
(^(?!.*\/\/)^(?!^\/)[A-Za-z0-9\/\-?:().,'+\s]+(?<!\/))
Negative look-behind expression not working in IE:
(?<!\/))
Thanks in advance and best regards.
If you don't want a / at the end of the string, you could add another lookahead.
^(?!.*\/\/)^(?!^\/)(?!.*\/$)[A-Za-z0-9\/\-?:().,'+\s]+
Regex demo
But in that case, it might be easier to use a version where the / is not in the character class but optionally repeated preceding the character class.
This way, it can not occur at the start or at the end of the string, and there can also not be //
^[A-Za-z0-9?:().,'+\s-]+(?:\/[A-Za-z0-9?:().,'+\s-]+)*$
Regex demo

regex with OR condition not working in angularjs [duplicate]

I'm creating a javascript regex to match queries in a search engine string. I am having a problem with alternation. I have the following regex:
.*baidu.com.*[/?].*wd{1}=
I want to be able to match strings that have the string 'word' or 'qw' in addition to 'wd', but everything I try is unsuccessful. I thought I would be able to do something like the following:
.*baidu.com.*[/?].*[wd|word|qw]{1}=
but it does not seem to work.
replace [wd|word|qw] with (wd|word|qw) or (?:wd|word|qw).
[] denotes character sets, () denotes logical groupings.
Your expression:
.*baidu.com.*[/?].*[wd|word|qw]{1}=
does need a few changes, including [wd|word|qw] to (wd|word|qw) and getting rid of the redundant {1}, like so:
.*baidu.com.*[/?].*(wd|word|qw)=
But you also need to understand that the first part of your expression (.*baidu.com.*[/?].*) will match baidu.com hello what spelling/handle????????? or hbaidu-com/ or even something like lkas----jhdf lkja$##!3hdsfbaidugcomlaksjhdf.[($?lakshf, because the dot (.) matches any character except newlines... to match a literal dot, you have to escape it with a backslash (like \.)
There are several approaches you could take to match things in a URL, but we could help you more if you tell us what you are trying to do or accomplish - perhaps regex is not the best solution or (EDIT) only part of the best solution?

Restricting file types in AngularJS with HTML input type="file"

So my HTML passes a file into AngularJS, and it is named as myFile.
So I use:
console.log($scope.myFile.type);
and it prints out 'application/pdf'.
But when I use this line:
if ($scope.myFile.type == 'application/pdf'){
// some stuff here
}
or
($scope.myFile.type == {'image/jpeg': fileMimeType})
Those will not ever be equal to true. I have no idea how to run this comparison anymore, and would appreciate snippets that would allow me to somehow create this comparison.
Thanks.
I wish i could make comments, but alas i do not have enough points. Did you try console.log( typeof $scope.myFile.type); to make sure it was a string? Also did the output itself have single quotes in it? because if it does have the quotes in it you'll either need to strip those or compare with the quotes in your string.

AugluarJS truncating trailing equal sign

AngularJS truncates the trailing equal signs when routing to them. I have a base64 string which need to be added as a query parameter to the url(route)
ex: http://example.com/#!/updatepassword?code=NnuW3q49QW38Mf-Cg==
this loads as:
http://example.com/#!/updatepassword?code=NnuW3q49QW38Mf-Cg
Is there a workaround for this?
If you try to set route as a string, then you need to escape = sign. That's because this character has a special meaning in the query string - it separates parameter name from its value.
So, one solution could be:
var query = "code=NnuW3q49QW38Mf-Cg==";
$location.url("/some/path?" + encodeURIComponent(query));
What encodeURIComponent() will do, is it will replace all special characters - = will be replaced with %3D for instance. This will prevent it from being interpreted as a key-value separator.
If you only want to change the query string parameters, not the whole URL, you can also use $location.search() method:
$location.search("code", "NnuW3q49QW38Mf-Cg==");
Just remember to pass two parameters to that method, not one. If you do:
$location.search("code=NnuW3q49QW38Mf-Cg==");
the = sign will not get escaped, only stripped.
Angular uses parseKeyValue() function internally to parse query string, it can be found here. You can see that the split is being done over the = sign. That's why they get stripped.
But if you take a look at .search() method implementation, you see that parseKeyValue() is being called only if you supply one argument to .search(). It's not invoked if you supply name of the parameter as a first, and value as a second argument.
Peeking at the source code also suggests yet another solution:
$location.search({"code": "NnuW3q49QW38Mf-Cg=="});

Concat strings with separator in velocity template

In my velocity template, I want to concat strings delimited by comma(,) using foreach loop.
#foreach($field in $MyObject.Fields)
${field.Name},
#end
This results in an extra comma at the end. How can avoid the last unwanted comma?
PS: I am using NVelocity 0.4.2
Thanks
I can't recall where NVelocity 0.4.2 comes from (i.e. Castle Project or the original port), but if it has the foreach looping directives, you can use them. Take a look at this recently asked question on the same topic:
NVelocity advance lopping syntax
If it doesn't have support for that, you can use the special $velocityCount variable with an #if directive to determine if you want a comma or not.

Resources