regex rule for email validation is not working in vuejs - vue.js

I tried this code as provided in https://v2.vuejs.org/v2/cookbook/form-validation.html.
validEmail: function (email) {
var re = /^(([^<>()\[\]\\.,;:\s#"]+(\.[^<>()\[\]\\.,;:\s#"]+)*)|(".+"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
Vuejs is saying that - error: Unnecessary escape character: \[ (no-useless-escape) at src/components/form.vue:125:65 which is the line var re = ...
What I am getting wrong here? I called this function like this.validEmail(this.modelname)

The "error" (technically it's just a warning) is saying that you do not need to escape [ in the regex when it is inside a character set ([] syntax) because its meaning is unambiguous (you can't create nested character sets); ], on the other hand, does need to be escaped because it will be interpreted as part of the regex syntax that ends the character set instead of a literal ] character.
Simplified example:
/[\[]/
^ unnecessary escape
should be instead:
/[[]/

Related

Java Parser comment statement

I am trying to comment a particulate statement.
My first approach is to return a comment in case statement is an 'Expression Statement' and expression is a particular 'Method Call Expression'.
new ModifierVisitor<Object>() {
public Visitable visit(ExpressionStmt expStmt, Object arg) {
Expression exp = expStmt.getExpression();
if (exp.isMethodCallExpr()) {
// My other logic goes here
return new LineComment(expStmt.toString());
}
}
}
But it failed while dumping the unit back to string.
java.lang.ClassCastException: com.github.javaparser.ast.comments.LineComment cannot be cast to com.github.javaparser.ast.stmt.Statement
at com.github.javaparser.printer.DefaultPrettyPrinterVisitor.visit(DefaultPrettyPrinterVisitor.java:1329)
at com.github.javaparser.printer.DefaultPrettyPrinterVisitor.visit(DefaultPrettyPrinterVisitor.java:163)
at com.github.javaparser.ast.stmt.BlockStmt.accept(BlockStmt.java:76)
at com.github.javaparser.printer.DefaultPrettyPrinterVisitor.visit(DefaultPrettyPrinterVisitor.java:1220)
at com.github.javaparser.printer.DefaultPrettyPrinterVisitor.visit(DefaultPrettyPrinterVisitor.java:163)
at com.github.javaparser.ast.body.MethodDeclaration.accept(MethodDeclaration.java:104)
at com.github.javaparser.printer.DefaultPrettyPrinterVisitor.printMembers(DefaultPrettyPrinterVisitor.java:190)
at com.github.javaparser.printer.DefaultPrettyPrinterVisitor.visit(DefaultPrettyPrinterVisitor.java:419)
at com.github.javaparser.printer.DefaultPrettyPrinterVisitor.visit(DefaultPrettyPrinterVisitor.java:163)
at com.github.javaparser.ast.body.ClassOrInterfaceDeclaration.accept(ClassOrInterfaceDeclaration.java:98)
at com.github.javaparser.printer.DefaultPrettyPrinterVisitor.visit(DefaultPrettyPrinterVisitor.java:325)
at com.github.javaparser.printer.DefaultPrettyPrinterVisitor.visit(DefaultPrettyPrinterVisitor.java:163)
at com.github.javaparser.ast.CompilationUnit.accept(CompilationUnit.java:133)
at com.github.javaparser.printer.DefaultPrettyPrinter.print(DefaultPrettyPrinter.java:104)
at com.github.javaparser.ast.Node.toString(Node.java:320)
As it suggests that you can replace a 'Statement' with another statement so instead I tried another approach to replace the statement and with an 'Empty Statement'. It kind of worked for me but the output does not look good as it leaves extra ';' after the commented line.
At third I tried to go deeper and instead of replacing the statement I tried to replace the expression with an comment. That too failed as mentioned in SO - Javaparser comment expression.
Any idea how to fix this ?
I tried a workaround which does not feel like a good solution, but gives me the expected result for now:
BlockStmt blockStmt = (BlockStmt) expStmt.getParentNode().get();
blockStmt.getStatement(blockStmt.getStatements().indexOf(expStmt) + 1).setLineComment(expStmt.toString());
return null;

Karate - String concatenation of JSON value with a variable [duplicate]

The embedded expressions are not replaced when appended, prepended or surrounded by characters in the following simplified and very basic scenario:
* def jobId = '0001'
* def out =
"""
{
"jobId": "#(jobId)",
"outputMetadata": {
"fileName_OK": "#(jobId)",
"fileName_Fail_1": "some_text_#(jobId)",
"fileName_Fail_2": "#(jobId)-and-some-more-text",
"fileName_Fail_3": "prepend #(jobId) and append"
}
}
"""
* print out
Executing the scenario returns:
{
"jobId": "0001",
"outputMetadata": {
"fileName_OK": "0001",
"fileName_Fail_1": "some_text_#(jobId)",
"fileName_Fail_2": "#(jobId)-and-some-more-text",
"fileName_Fail_3": "prepend #(jobId) and append"
}
}
Is it a feature, a limitation, or a bug? Or, did I miss something?
This is as designed ! You can do this:
"fileName_Fail_2": "#(jobId + '-and-some-more-text')"
Any valid JS expression can be stuffed into an embedded expression, so this is not a limitation. And this works only within JSON string values or when the entire RHS is a string within quotes and keeps the parsing simple. Hope that helps !

how to remove all html characters in snowflake, dont want to include all html special characters in query (no hardcoding)

Want to remove below kind of characters from string..pl help
'
&
You may try this one to remove any HTML special characters:
select REGEXP_REPLACE( 'abc&def³»ghi', '&[^&]+;', '!' );
Explanation:
REGEXP_REPLACE uses regular expression to search and replace. I search for "&[^&]+;" and replace it with "!" for demonstration. You can of course use '' to remove them. More info about the function:
https://docs.snowflake.com/en/sql-reference/functions/regexp_replace.html
About the regular expression string:
& is the & character of a HTML special character
[^&] means any character except &. Tthis prevents to REGEXP to replace all characters between the first '&' char and last ';'. It will stop when it see second '&'
+ means match 1 or more of preceding token (any character except &)
; is the last character of a HTML special character
CREATE or REPLACE FUNCTION UDF_StripHTML(str varchar)
returns varchar
language javascript
strict
as
'var HTMLParsedText=""
var resultSet = STR.split(''>'')
var resultSetLength =resultSet.length
var counter=0
while(resultSetLength>0)
{
if(resultSet[counter].indexOf(''<'')>0)
{
var value = resultSet[counter]
value=value.substring(0, resultSet[counter].indexOf(''<''))
if (resultSet[counter].indexOf(''&'')>=0 && resultSet[counter].indexOf('';'')>=0)
{
value=value.replace(value.substring(resultSet[counter].indexOf(''&''), resultSet[counter].indexOf('';'')+1),'''')
}
}
if (value)
{
value = value.trim();
if(HTMLParsedText === "")
{
HTMLParsedText = value
}
else
{
if (value) {
HTMLParsedText = HTMLParsedText + '' '' + value
}
}
value=''''
}
counter= counter+1
resultSetLength=resultSetLength-1
}
return HTMLParsedText';
to call this UDF :
Select UDF_StripHTML(text)

Jeval EvaluationException: Invalid use of quotes

When I try to evaluate an expression I get this error: net.sourceforge.jeval.EvaluationException: Invalid use of quotes.
To be clearer. I read my rules from a .txt files where after I put them in a hasmap.
I have tried:
#{mentor} = Chung
#{mentor} = "Chung"
#{mentor} == "Chung"
#{mentor} == 'Chung'
but still gives another error or same error.
What am I doing wrong?
List<String> approvalMentors = new ArrayList<>();
Evaluator evaluator = new Evaluator();
evaluator.putVariable("mentor", String.valueOf(graduationApprovalRequest.getMentor()));
for (HashMap.Entry<String, String> entry : this.schoolRules.entrySet()) {
try {
if(evaluator.evaluate(entry.getValue()).equals("1.0")){
approvalMentors.add(entry.getKey());
}
} catch (EvaluationException ex) {
Logger.getLogger(SchoolRules.class.getName()).log(Level.SEVERE, null, ex);
}
}
return approvalMentors;
.txt File I read my rules from
#{mentor} = 'Chung'
JEval internal validation logic assumes each "operand" (a part of the conditional expression) should be either a built-in function, a number, a special symbol, OR a quoted string. If there is even a single SPACE between the == and double-quote symbols, the check for correctness of quoted string fails. Each such operand should either begin AND end with a quote(s), or do not have the quote(s) at all. The latter is the case with numbers, for example.

How to filter out some vulnerability causing characters in query string?

I need to filter out characters like /?-^%{}[];$=*`#|&#'\"<>()+,\. I need replace this with empty string if it is there in the query string. Please help me out. I am using this in ASP pages.
Best idea would be to use a function something along the lines of:
Public Function MakeSQLSafe(ByVal sql As String) As String
'first i'd avoid putting quote chars in as they might be valid? just double them up.
Dim strIllegalChars As String = "/?-^%{}[];$=*`#|&#\<>()+,\"
'replace single quotes with double so they don't cause escape character
If sql.Contains("'") Then
sql = sql.Replace("'", "''")
End If
'need to double up double quotes from what I remember to get them through
If sql.Contains("""") Then
sql = sql.Replace("""", """""")
End If
'remove illegal chars
For Each c As Char In strIllegalChars
If sql.Contains(c.ToString) Then
sql = sql.Replace(c.ToString, "")
End If
Next
Return sql
End Function
This hasn't been tested and it could probably be made more efficient, but it should get you going. Wherever you execute your sql in your app, just wrap the sql in this function to clean the string before execution:
ExecuteSQL(MakeSQLSafe(strSQL))
Hope that helps
As with any string sanitisation, you're much better off working with a whitelist that dictates which characters are allowed, rather than a blacklist of characters that aren't.
This question about filtering HTML tags resulted in an accepted answer suggesting the use of a regular expression to match against a whitelist: How do I filter all HTML tags except a certain whitelist? - I suggest you do something very similar.
I'm using URL Routing and I found this works well, pass each part of your URL to this function. It's more than you need as it converts characters like "&" to "and", but you can modify it to suit:
public static string CleanUrl(this string urlpart) {
// convert accented characters to regular ones
string cleaned = urlpart.Trim().anglicized();
// do some pretty conversions
cleaned = Regex.Replace(cleaned, " ", "-");
cleaned = Regex.Replace(cleaned, "#", "no.");
cleaned = Regex.Replace(cleaned, "&", "and");
cleaned = Regex.Replace(cleaned, "%", "percent");
cleaned = Regex.Replace(cleaned, "#", "at");
// strip all illegal characters like punctuation
cleaned = Regex.Replace(cleaned, "[^A-Za-z0-9- ]", "");
// convert spaces to dashes
cleaned = Regex.Replace(cleaned, " +", "-");
// If we're left with nothing after everything is stripped and cleaned
if (cleaned.Length == 0)
cleaned = "no-description";
// return lowercased string
return cleaned.ToLower();
}
// Convert accented characters to standardized ones
private static string anglicized(this string urlpart) {
string beforeConversion = "àÀâÂäÄáÁéÉèÈêÊëËìÌîÎïÏòÒôÔöÖùÙûÛüÜçÇ’ñ";
string afterConversion = "aAaAaAaAeEeEeEeEiIiIiIoOoOoOuUuUuUcC'n";
string cleaned = urlpart;
for (int i = 0; i < beforeConversion.Length; i++) {
cleaned = Regex.Replace(urlpart, afterConversion[i].ToString(), afterConversion[i].ToString());
}
return cleaned;
// Spanish : ÁÉÍÑÓÚÜ¡¿áéíñóúü"
}