cancel
Showing results for 
Search instead for 
Did you mean: 

How to make a regular expression case insensitive?

02-26-2014 8:02 AM
1400 views 3 comments Go to solution
0 Likes
SAP Managed Tags
Subscribe

Hi,

I am using SAP UI5 .

I am validating the user input using a regular expression to make sure user dose not enter a keyword which is reserved.

var regexAppID = /^(?=^(?:(?!(^Admin$|^AdminData$|^Push$|^smp_cloud$|^resource$|^test-resources$|^resources$|^Scheduler$|^odata$|^applications$|^Connections$|^public|^Public$|[\.]{2,}|^[\.])).)*$)(?=^[a-zA-Z]+[a-zA-Z0-9_\.]*$).*$/;

textField.mBindingInfos.value.type.oConstraints.search = regexAppID;

The regex blocks the user from entering the keywords Admin,AdminData etc.

It works fine if the user enters the value as it is given in regex (ie case sensitive example "Admin").

But when user enters "admin" or "aDmin" etc the regex is not catching it and my server crashes as it bypasses these keywords.

0 Likes

Accepted Solutions (1)

Accepted Solutions (1)

WouterLemaire
SAP Mentor
SAP Mentor
0 Likes

Why don't you just use this?

/Admin|AdminData|resource|test-resources|resources|Scheduler|applications|public|Public/i


The i will ignore all cases.

You can generate regex:

JavaScript Lab - Tools - JS Regex Generator

And for testing you can use this:

Rubular: a Ruby regular expression editor and tester

Kind regards,

Wouter

Former Member
0 Likes

Hi Wouter,

Using "/i" like below works fine. It restricts the words.

var regEx =  /^(?=^(?:(?!(^Admin$|^AdminData$|^Push$|^smp_cloud$|^resource$|^test-resources$|^resources$|^Scheduler$|^odata$|^applications$|^Connections$|^public|^Public$|[\.]{2,}|^[\.])).)*$)/i;

But when I add (?=^[a-zA-Z]+[a-zA-Z0-9_\.]*$).*$/ to above which allow user to enter any string with starting alphabet, it gives me syntax error.

var regEx = /^(?=^(?:(?!(^Admin$|^AdminData$|^Push$|^smp_cloud$|^resource$|^test-resources$|^resources$|^Scheduler$|^odata$|^applications$|^Connections$|^public|^Public$|[\.]{2,}|^[\.])).)*$)/i (?=^[a-zA-Z]+[a-zA-Z0-9_\.]*$).*$/ ;

WouterLemaire
SAP Mentor
SAP Mentor
0 Likes

Can't you use this expression?

^/(?!ignoreme$)(?!ignoreme2$)[a-z0-9]+$

Then you will have only a match when the word is something else then "ignoreme" or "ignorme2".

Why don't you just check it in an IF statement? if(input==="ignoreme" or ... ) ? It's both static.. or you could put all options in an array and use the statement indexOf to find if it is in the array.

JavaScript Array indexOf() Method

Kind regards,

Wouter

Answers (0)