Simple form issue applying custom regexp (Rails 4) - javascript

I'm having some issues validating fields in Rails using simple_form gem. The issue is that, when using a custom regexp in Rails to validate in the server, everything goes fine, but the javaScript takes the whole regexp (including the tags \A or \z so it appears like wrong. For example, my validation expression is:
validates_format_of [:field1, :field2], :with => /\A(myregexp)\z/i
But in the HTML code, is sent like this, so the JavaScript doesn't take the right expression:
pattern="\A(myregexp)\z"
I know that JavaScript doesn't accept \A or either \z, but also I can't use the old tags for rails ^ or $ cause it shows an error.
Any ideas of how can I fix this?

Related

Angular server side rendering template (.cshtml) parse error with escape symbols

I have C# razor *.cshtml template file and would like to use it for Angular (5) server side rendering.
C# .cshtml snippet:
#helper RenderLookupComponent()
{
<list-lookup
[data]="getLookupData('#Model.LookupSourceType')"
[lookupType]="'#Model.LookupSourceType'">
</list-lookup>
}
Everything works fine until '#Model.LookupSourceType' value is not contains escape symbol ('\'), for e.g. when #Model.LookupSourceType = 'SomeType\'
Angular template parser Error:
[error]: Template parse errors:
Parser Error: Missing expected ) at column # in [getLookupData('SomeType\')]
At first glance, it looks like ordinary issue, actually it can be solve by replacing all '\' to '\'. (#Model.LookupSourceType.Replace(#"\", #"\\")).
Q: But i believe there should be more elegant, universal approaches to solve such kind of problems.
The approach that can handles all possible scenario for different kind of escape symbols
Please share your experience
Problem was solved with using HttpUtility.JavaScriptStringEncode(Model.LookupSourceType)

Syntax error on regular expression in ExtendScript (Javascript ECMA-262 — Verison 3) [duplicate]

I have a regular expression testing for numbers(0-9) and/or forward slashes (/). It looks like this:
/^[0-9/]+$/i.test(value)
Now I believe this to be correct, but the eclipse javascript validator disagrees:
Syntax error on token "]", delete this token
I suppose this is because the separator/delimiter is / and eclipse 'thinks' the regex is finished (and therefore a ] would be unexpected).
We can satisfy eclipse by escaping the / like so:
/^[0-9\/]+$/i.test(value)
Note that both versions work for me.
My problem with this is:
As far as I know I do not need to escape the forward slash specifically in that range. It might be situation specific (as in, for javascript it is the used delimiter).
Although they both appear to be working, I'd rather use the 'correct' version because of behaviour in different environments, and, well.. because correct and all :)
Does anyone know what I'm supposed to do? Escape or not? I did not find any reputable site that told me to escape the / in a range, but the Eclipse-validator is probably not completely stupid...
The standard clearly says you can put anything unescaped in a character class except \, ] and newline:
RegularExpressionClassChar ::
RegularExpressionNonTerminator but not ] or \
RegularExpressionBackslashSequence
RegularExpressionNonTerminator ::
SourceCharacter but not LineTerminator
( http://es5.github.com/#x7.8.5 ). No need to escape /.
On the other side, I personally would escape everything when in doubt, just to make less smart parsers happy.

Debugging a Javascript RegExp Regular Expression: Validation Hangs on Amazon URL

In using Javascript to validate a URL, I used the following code from an SO answer:
function validateURL(textval) {
var urlregex = new RegExp(
"^(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*#)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$");
return urlregex.test(textval);
}
This function works fine for most URLs I tried, but on the following Amazon url to (ironically) a Creating Mobile Apps with jQuery Mobile book, it hangs. In Chrome dev tools I see nothing but clicking anywhere inside the tab doesn't do anything.
http://www.amazon.com/gp/product/178216006X/ref=s9_simh_gw_p351_d3_i4?pf_rd_m=ATVPDKIKX0DER&pf_rd_s=center-2&pf_rd_r=0SDC8SED1N96XPK44VD2&pf_rd_t=101&pf_rd_p=1389517282&pf_rd_i=507846
The URL is pretty long but there's nothing special in there that I can tell. In fact it passes the Javascript validation on the following Scott's Playground page.
My question is NOT how to do URL validation in Javascript. My question is the following: If I use a Javascript regular expression and it hangs on a piece of text, what makes a regular expression freeze the browser like this? How can I catch the cases that does that?
Is this something that only happens with new RegExp(...) vs /regex/ as mentioned in this answer?
In terms of the actual validation, I switch to a different /regex/ but I still wanted to post this question because it led to a pretty painful debugging process. (Then again, anything that tries to validate URLs or emails with regular expressions will probably be painful).
This seems to be something that happens with new RegExp(...) and not with /regex/ for this regular expression. So for URL validation and other types of regex matching, use:
function validFoo(value) {
return /foo/i.test(value);
}
Where foo is the regular expression.
It looks like there's a problem with the regular expression and how Chrome interprets it. In Firefox it works.
The issue lies at the second request parameter in your url (the &) and how the Chrome javascript engine gets stuck in a loop.
If you don't need to evaluate the port in the url use something like this:
/^(https?://)?([\da-z.-]+).([a-z.]{2,6})([/\w .-])/?$/

Get everything but dashes at the beginning - regEx in javascript

I'm trying to build my regular expression, but I've failed.
I'm trying to get everything after single or double dash (you can try it here):
var regEx = /(?<=[-{1,2}])[^-]\S*/g;
It works just fine, but it selects even if we have 3+ dashes too. I've tried something like /(?<=^[-{1,2}])[^-]\S*/g and /(?<=\b[-{1,2}])[^-]\S*/g, but then it crashes at all.
Thanks in advance.
Unfortunately javascript doesn't support lookbehind
You can use this regex with multiline option
^-{1,2}(?!-)(\S*)
After this you can use group 1 to access the required match..

CKEDITOR.instance[x].setData not working in IE

Ok, I'm using the CKEditor in a web application. One thing I need to do it set the text in the text area. I've been using the line:
CKEDITOR.instances.setData(html);
...where html is a varible containing HTML.
This works fine in Chrome & Firefox, but not at all in Internet Explorer or Safari.
Can anyone provide an insight as to why, or suggest a work-around?
Many thanks in advance! :-)
Make sure to strip all newlines from the string you pass into setData(). An exception is thrown if you don't, with a message about an unterminated string. The newline characters used by CKEditor are the UNIX-style of \n (in other words, not the DOS version: \r\n).
The newline apparently throws off the parser, making it think that it's the end of the statement.
Also note that if you call getData() to get that value you just set again, CKEditor puts the line breaks and tabs back into it. You'll need to strip them out again if you need to set that value back using setData(). I use a regexp pattern like this to strip out the newlines (and tabs just for completeness):
[\n\t]+
Also make sure that if you use the regular expression to strip them, you need to make sure that the pattern matching will match the \n character (called "single-line" mode in .NET, but I don't know what you're using).

Categories