is it possible to create a sweetjs macro making multiline strings possible in javascript? - sweet.js

I am really new to sweet.js.
I would love to have multiline strings in javascript just like in EcmaScript 6:
var htmlString = `Say hello to
multi-line
strings!`;
Is it possible to formulate a sweetjs macro handling that (and how ??) ?
Furthermore, would it be possible to do String Interpolation/Templating using that macro just like ES6 does ?

There's an example at the bottom of this issue on the GitHub project that uses backticks.

Related

How to make this regular expression Pattern case insensitive - for list of words [duplicate]

Given this regular expression: "^[0-9]*\s*(lbs|kg|kgs)$" how do I make it case insensitive? I am trying to use this in a .net regular expression validator, so I need to specify case insensitivity in the pattern.
I can not use the RegexOptions programatically because I am specifying the regular expression in a RegularExpressionValidator
I found out.
Case sensitive: ^[0-9]\s(lbs|kg|kgs)$
Case insensitive: (?i:^[0-9]\s(lbs|kg|kgs)$)
I believe that this is specific to the .NET implementation of regular expressions. So if you use this in the RegularExpressionValidator you have to turn off client side validation because the javascript regex parser will not recognize the ?i token.
Here is an alternative using a CustomValidator, when really needing case-insensitivity server-side and client-side; and the the Upper/lower [A-Za-z] char approach is too much.
This blends the various other answers, using the server-side RegEx object and client-side JavaScript syntax.
CustomValidator:
<asp:CustomValidator ID="cvWeight" runat="server" ControlToValidate="txtWeight"
OnServerValidate="cvWeight_Validate" ClientValidationFunction="cvWeight_Validate"
ValidateEmptyText="true" Text="*" ErrorMessage="Invalid entry." />
Code behind:
protected void cvWeight_Validate(object sender, ServerValidateEventArgs args)
{
Regex re = new Regex(#"^[0-9]*\s*(lbs|kg|kgs)$", RegexOptions.IgnoreCase);
args.IsValid = re.IsMatch(args.Value);
}
Client-side validation function:
function cvWeight_Validate(sender, args) {
var reWeight = /^[0-9]*\s*(lbs|kg|kgs)$/i;
args.IsValid = reWeight.test(args);
}
This is working fine for me, except when using a ValidationSummary. On the client-side validation, the error * shows, but I can't get the error message to display in the summary. The summary only displays when submitted. I think it's supposed to display; I have a mix of update panels and legacy code, which may be problems.
Can we make Regex case-insensitive in C#? : Yes
Set option inline via (?i) pattern construct or via RegexOptions.IgnoreCase param
Can we make Regex case-insensitive in JavaScript? : Yes
Set flag via /pattern/flags syntax or the insensitive flag /REGEX/i
(Aside) Can we make Regex case-insensitive in HTML5 Pattern Attribute? : No
As Chris R. Timmons points out on RegularExpressionValidator doesn't ignore case:
There is no property in the RegularExpressionValidator control that
allows regex options to be set.
If your control does both client-side and server-side validation, the
regex must use a subset of regular expression syntax that both
JS and .NET can execute. In this case, to make a regex ignore
case it is necessary to use a character class construct like [a-zA-Z]
to match both upper and lower case characters.
If your validation is done on the server-side only, you can use the
more powerful .NET regular expression syntax. In this case, you can
place the (?i) option at the beginning of the regex to tell it to
ignore case.
So, if you want to use the out of the box validator, you're stuck with Geoff's solution of using character sets like this: [aA]
Easiest here is to just modify the regex to
^[0-9]*\s*([lL][bB][sS]|[kK][gG][sS]?)$
It's awful to read, but it will work fine.

How to add stripslashes to get_option()

Hy, I created a WordPress Theme using a lot of custom options. While saving the options in the backend amongst other things I'm adding backslashes before characters that need to be escaped. Such as ' and ".
Now I need to remove them before displaying them on the frontend. The easiest way would be stripslashes(get_option($key)). But that would mean I'd have to go though the whole Theme and change all get_option() manually.
Is there a way to add a filter to the get_option()?
If not, is there a way to achieve this with find/replace (I'm using Sublime Text 3 which allows regex)?
Why not just create your own function in place of get_option() (but which takes advantage of it)? For example, you could define the following in functions.php:
function my_stripslashes_function($option, $default = false) {
return stripslashes( get_option($option, $default) );
}
And then use Sublime to replace all instances of get_option with my_stripslashes_function...no regex required, and it's more DRY.
Yes in Sublime Text you can realize it with an regex:
find: get_option\((.[^)]*)\)
replace with: stripslashes(get_option($1))
If you need to debug a regex yourself here is a helpful tool: https://regex101.com/r/rY7oG6/2

Regular Expression is working for php not for JavaScript

This is my expression:
^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])[[:graph:]]{6,25}$
I have tested it on http://regex101.com/
Its working for string DeepakManwal1 when there is php selected but does not work when javascript is selected. I don't know what is the exact reason.
I want to use this expression for password validation where there should be at-least one uppercase letter and one numeric character.
Here is fiddle http://jsfiddle.net/j7rmj44h/
It is because of the POSIX class [:graph:] — you could change it to the equivalent [\x21-\x7E]. Also, you need to remove the quotations around your pattern according to your fiddle.
var re = /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])[\x21-\x7E]{6,25}$/
Fiddle
AFAIK, javascript doesn't understand POSIX.
Have a try with:
^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])\S{6,25}$
Different regex engines have different capabilities. Only the simplest of regex can be shared across implementations.
If I recall correctly [:graph:] like specification of character classes is not supported in JS.

C# & PLSQL - Finding Comments and Strings by RegExp

Is it possible to distinguish between code, strings and comments by using regular expressions?
I'm trying to build a formatter for PLSQL-Code in C# and I can't manage to solve the problem, that strings, respectively quotes ( " or ' ) can also appear in comments, negating their special character and vice versa, comment indicators ( -- or /* ) appearing in strings, being nullified as a special character sequence.
Is there a solution, or are regular expressions simply the wrong way to handle this?
Best regards,
Philipp
You might find an idea how to reliably identify comments by studying a source code of Perl module Regexp::Common::comment. The module supports matching also PL/SQL comments.

how to detect links in a string using RegEx in as3?

I am trying to find the generic links in strings. I've found a very handy regex on RegExr, in the community expressions:
(https?://)?(www\.)?([a-zA-Z0-9_%]*)\b\.[a-z]{2,4}(\.[a-z]{2})?((/[a-zA-Z0-9_%]*)+)?(\.[a-z]*)?(:\d{1,5})?
I tried to use it and it returns null, although the same string tested on RegExr works fine:
var linkRegEx:RegExp = new RegExp("(https?://)?(www\.)?([a-zA-Z0-9_%]*)\b\.[a-z]{2,4}(\.[a-z]{2})?((/[a-zA-Z0-9_%]*)+)?(\.[a-z]*)?(:\d{1,5})?","g");
var link:String = 'generic links: www.google.com http://www.google.com google.com';
trace(linkRegEx.exec(link));//traces null
Is there anything I'm missing ?
you need to double the backslashes when you're using new RegExp. you might want to use the literal syntax, which doesn't impose such a requirement (assuming AS3 admits this syntax, I just know JS.
Looks like maybe you're trying to match the wrong variable? In line linkRegEx.exec(formattedStatus), formattedStatus isn't defined.