I know I can do this...
RegExp.prototype.xtend = function(){ console.log(123) }
y = new RegExp(/pattern/)
y.xtend()
I want to extend regex patterns in this format: /pattern/, so I can do...
/pattern/.xtend()
Edit: It was a problem with syntax
Turns out it was a missing semi-colon. I need a semi-colon before the regex pattern otherwise it gives syntax error.
Thanks for all the comments. Works great now.
Turns out it was a missing semi-colon. I need a semi-colon before the regex pattern otherwise it gives syntax error.
I was running this in Firebug console, which automatically returns the value of the last line... but because there is no semi-colon after the third line, and the fourth starts with a regex, it was getting an error.
RegExp.prototype.xtend = function(){ console.log(123) }
y = new RegExp(/pattern/)
y.xtend()
/pattern/.xtend()
Adding the semi-colon solves the problem...
RegExp.prototype.xtend = function(){ console.log(123) }
y = new RegExp(/pattern/)
y.xtend(); // <~~ this semi-colon fixes the issue
/pattern/.xtend()
Thanks for all the comments. Works great now.
Related
I use winston to perform logging currently and have written a common method for it to be used all over project. Problem is, many of logging statements are like, logger.info("here is the data" , data)
With comma as concatenator, i couldn't log data in console. data can also be a content containing comma so I wouldn't be able to just use replace ',' by '+' .
My idea is regex can be like, if text starts with ' or " and its next character is ',' at end of quotes, replace with '+'
Ain't sure if it would be right but still, please help with your suggestions.
Perhaps you can monkey-patch it with something like
logger.info = ((infoFunc) => {
// create the patched info that concatenates all arguments
// before calling the original logger.info
let patch = () => {
// put any logic here that you need to achieve the desired result.
var message = args.join('');
infoFunc(message);
};
return patch;
})(logger.info);
Not tested
Just make sure it's run right after the logger is set up.
This will work as a quick fix to get things running but I wouldn't recommend leaving it in your code and should be removed once all calls to logger.info have been cleaned up.
I have been working on my project where I had to do some updates on my data records. After I finished my update I got an error: SyntaxError: identifier starts immediately after numeric literal and this line of code was below that error in firebug : maxScores.ew-19a = ''
I looked up in my code and I found where is this output coming from, here is the code:
var maxScores = new Object;
<cfoutput query="getRec">maxScores.#LCase(tCode)# = '#maxScore#';</cfoutput>
In my update I had to put - symbol between letter and number, in old data I did not have that so I think that causing the problem here. I was wondering how I can prevent this or if there is any method that I have to put around my output to prevent this? If you know how this can be fixed pleas let me know. Thank you.
ColdFusion will attempt to subtract ew from 19a when you just dump it between to pound signs like that. You will need to use bracket/object notation here. Try this:
<cfoutput query="getRec">
maxScores.#LCase(getRec["tCode"][currentrow])# = '#getRec["maxScore"][currentrow]#';
</cfoutput>
If you want to Lower case something do it in the query. Outputing JS using CF is useful and solves many problems, but you want to keep it as clean as possible to keep your brain from fogging over. :)
I have written the following regex to catch anything within two parenthesis of a text:
var rule = /[a-zA-Z0-9]*(\(+[a-zA-Z\s0-9]*\))*[a-zA-Z0-9\s]/;
It works in this website: http://regex101.com/ (javascript section)
Look the right side of the window and you see the bottom section shows the matched string which is what I want.
But in firebug when I execute the following code it crashes. Why? how then should I catch the group in parenthesis?
var rule = /^[a-zA-Z0-9\s]*(\(+[a-zA-Z\s0-9]*\))*[a-zA-Z0-9\s\,]*/;
var str = "He left us (with anger), but came back all cool and modest.";
var res = "";
while((res = rule.exec(str)) !== null)
{
console.log("Good");
}
console.log(res);
Or maybe I'm totally wrong and am missing something?
"But in firebug when I execute the following code it crashes. Why?"
To use .exec() in a loop like that, the regex must be global. Otherwise it's just going to keep getting the first match, resulting in an infinite loop.
// make it global ------------------------------------------------v
var rule = /^[a-zA-Z0-9\s]*(\(+[a-zA-Z\s0-9]*\))*[a-zA-Z0-9\s\,]*/g;
When global, the regex object becomes stateful, and each subsequent .exec() will begin from the character after the last match.
When there are finally no matches left, it'll return null, breaking the loop.
As others pointed out in the comments, the leading ^ will guarantee only one match (without the m modifier anyway), so you'll probably want to remove that. It's not the cause of the crash though.
I'm cobbling together a script which maps mouse X,Y coordinates to an axis grid. The resulting variable will then be passed to CSS transform property. I'm getting all the numbers I need, but I'm stuck on the last part, which is to remove the \ around the result, which has been converted to a regular expression so as to allow for negative integers.
var resultX = RegExp(Math.round(mousePos.x/6.6) -60);
resultX = resultX.replace(/\//g,'');
The final stage (stripping the slashes) throws an error no matter how I do it. I've tried encapsulating .replace in a function, and using return, but I continue to get the same error:
TypeError: 'undefined' is not a function (evaluating
'resultX.replace(///g,'')')
I'm stuck, and haven't been able to find the solution anywhere. Perhaps the problem is that my variable isn't a true string? Or maybe someone has a suggestion for a better way to allow for negative integers.
JS fiddle:
http://jsfiddle.net/wAKnY/
Like JayC mentioned in the comments, there doesn't seem to be a reason to convert to a RegExp in the first place, so I would just recommend removing the enclosing RegExp().
However, if you require that for some reason that isn't apparent here, you can then call toString() on it to enable the replace function to behave correctly:
resultX = resultX.toString().replace(/\//g,'');
As JayC said, you don't need a regexp in the first place.
var resultX = (Math.round(mousePos.x/6.6)-60).toString;
This works:
resultX = resultX.toString().replace(/\//g, '');
resultY = resultY.toString().replace(/\//g, '');
The continuous integration software I am using runs JavaScript files through JSLint and complains if they fail (using default JSLint settings it appears).
I've always prepended a ; to the start of my jQuery plugins, so concatenating them doesn't lead to something like this...
common.js
I don't have access to this file, and can't enforce a semi colon at the end.
var abc = function() {
alert(arguments[0]);
}
plugin.js
This is my file that is concatenated with common.js. It is appended straight to the end of common.js.
(function($) {
$.fn.somePlugin = function() {}
})(jQuery);
jsFiddle of the problem this can cause.
jsFiddle of the solution (leading semi-colon of my plugin).
However, JSLint complains with...
Error:
Problem at line 1 character 1: Unexpected space between
'(begin)' and ';'.
;(function($) {
Problem at line 1 character 1: Expected ';' at column 5, not column 1.
;(function($) {
Problem at line 1 character 2: Missing space between ';' and '('.
...
I tried using a bang operator (!) instead, and a few other alternatives, but JSLint still complained.
What can I do to get this safety net and pass JSLint?
Patch JSLint to not mind. Everyone will benefit.
Possibly a flippant answer, but your html can probably do this:
<script src="common.js"></script>
<script>;</script>
<script src="plugin.js"></script>
Or if you don't like the inline script on the second line, make a file called semicolon.js and, well, you know....
Sorry if this is ridiculous, but JSLint does not like a plain old empty statement on a line by itself. Somewhat unfortunate. Oh well.
BTW awesome fiddle. Seeing that alert run because of the missing semicolon after the var declaration of the function was, like, wow what do you know? That function got called as it should! Very interesting.
What happens if you put the ; on a line by itself, or put some other syntactically valid but meaningless statement like this:
"begin plugin";
(function() { // etc
EDIT: what about using void:
void("begin plugin");
(function() { // etc
//or
void(
(function() { /* your code */ })()
);
EDIT 2: what about some variation on this:
if (console && console.log) { // or if (false)?
console.log("Begining plugin definition");
}
(function { // etc