in my regular expression, I tried to remove all "{" and "}"s from a string.
Pushing the script with packer/minimizer scripts, breaks them.
That's why I'd like to know about a better and more compatible way of writing:
mystring.replace(/\{/g,"");?
You can just use a string instead of a regex. I'm not sure if this is "better" but it should not break when minified. If you provide the minified example, we may be able to help with that.
mystring.replace("}", "").replace("{", "");
Edit:
If the curly bracket is causing the problem, perhaps this would work...
var reg = new RegExp("\\{|\\}", "g");
mystring.replace(reg, "");
Example from the console...
> var mystring = "test{foo}bar{baz}";
> var reg = new RegExp("\\{|\\}", "g");
> mystring.replace(reg, "");
"testfoobarbaz"
Lastly, you could do this:
If a regex really wont work for you, this will replace all {'s and }'s
It is probably a horrible solution, considering performance, but...
mystring.split("}").join("").split("{").join("");
You could try
mystring.replace(/\u007B/g,"");
This uses unicode rather than the actual symbol, so your packer won't get confused. If you want to replace more than one character in a single statement, you can use the "or" pipe:
mystring.replace(/\u007B|\u007D/g,"");
{ = \u007B
} = \u007D
For more unicode codes see:
http://www.unicode.org/charts/PDF/U0000.pdf
After re-reading the question, it sounds like you've found a bug with the minifier/packer. My first suggestion would be to use a better minimizer that doesn't have these issues, but if you're stuck with what you're using, you could try using the unicode escape sequence in the regular expression:
mystring.replace(/\u007b/g, '');
Alternatively, you could try String.prototype.split and Array.prototype.join:
mystring.split('{').join('');
Related
I'm trying to find out if a string contains css code with this expression:
var pattern = new RegExp('\s(?[a-zA-Z-]+)\s[:]{1}\s*(?[a-zA-Z0-9\s.#]+)[;]{1}');
But I get "invalid regular expression" error on the line above...
What's wrong with it?
found the regex here: http://www.catswhocode.com/blog/10-regular-expressions-for-efficient-web-development
It's for PHP but it should work in javascript too, right?
What are the ? at the start of the two [a-zA-z-] blocks for? They look wrong to me.
The ? is unfortunately somewhat overload in regexp syntax, it can have three different meanings that I know of, and none of them match what I see in your example.
Also, your \s sequences need the backslash escaping because this is a string - they should look like \\s. To avoid escaping, just use the /.../ syntax instead of new Regexp("...").
That said, even that is insufficient - the regexp still produces an Invalid Group error in Chrome, probably related to the {1} sequences.
The ?'s are messing it up. I'm not sure what they are for.
/\s[a-zA-Z\-]+\s*:\s*[a-zA-Z0-9\s.#]+;/
worked for me (as far as compiling. I didn't test to see if it properly detected a CSS string).
Replace the quotes with / (slashes):
var pattern = /\s([a-zA-Z-]+)\s[:]{1}\s*([a-zA-Z0-9\s.#]+)[;]{1}/;
You also don't need the new RegExp() part either, which is why it's been removed; instead of using a quote or double quote to denote a string, JavaScript uses a slash / to denote a regular expression, which isn't a normal string.
That regular expression is very bad and I would avoid its source in the future. That said, I cleaned it up a bit and got the following result:
var pattern = /\s(?:[a-zA-Z-]+)\s*:\s*(?:[^;\n\r]+);/;
this matches something that looks like css, for example:
background-color: red;
Here's the fiddle to prove it, though I'd recommend to find a different solution to your problem. This is a very simple regex and it's not save to say that it is reliable.
var input = '/string/';
var output = input.replace(/\//g,'');
// requested output = 'string';
What I don't like about this method is that the double slashes might be considered a comment and ignored by certain browsers, or am I wrong?
It works in my browser, but I can't test in every browser. Is it a perfectly proper method, or is there a better solution?
What I don't like about this method is that the double slashes might be considered a comment and ignored by certain browsers
What double slashes? The ones in the regexp? Certainly not, unless the browser's JavaScript parser is totally broken.
Is it a perfectly proper method
Yes.
That is the correct way to do the replacement. The double slashes won't be interpreted as a comment by any production browser; that regex form is a valid part of EMCAScript.
It should be alright but if you're really concerned, use a RegExp object, eg
var re = new RegExp("/", "g");
var output = input.replace(re, '');
Yes, the syntax is correct. It will work with any JavaScript engine.
I'm using a regExp in my project but some how I'm getting some undesirable characters
my RegExp looks like this:
new RegExp("[א-ת,A-z,',','(',')','.','-',''']");
which supposed to avoid characters like \ or []
but let my use one and more from (,),-,alphabets etc.
Unfortunately it doesnt happen
Which pattren includes both desirable and undesirable characters??
thanks for your help
Well your regular expression just says to match one "good" character (and incorrectly at that).
I think something closer to this would be what you want, though I'm not sure about the higher-page UTC characters:
var regexp = /^[א-תA-Za-z,()\-']*$/;
If the alefbet part doesn't work (it looks backwards to me, but I guess that's kind of a conundrum :-), try:
var regexp = /^[\u05DA-\05EAA-Za-z,()\-']*$/;
Might be good to tack an "i" (ignore case) modifier on the end too:
var regexp = /^[\u05DA-\05EAA-Za-z,()\-']*$/i;
This also does not handler the various diacritical marks; I don't know if you need those matched or not.
First of all, you don't need all those single quotes and commas. Second, you want A-Za-z, not.A-z. The latter includes ASCII characters between "Z" and "a".
var re = new RegExp("[א-תA-Za-z,()\.'\s-]");
I'm working with jQuery and am trying to write a pattern replace, but it doesn't work. I have this:
var $featured_rewrite = $('#featured').not('.slideshow');
$featured_rewrite.children().attr('href', $featured_rewrite.find('img').attr('src').replace('/-[0-9]+x[0-9]+\./i', '.'));
I don't understand why something like this works:
.replace('-500x277.', '.')
but not this, which I even checked with a tool and made sure it was valid and works:
.replace('/-[0-9]+x[0-9]+\./i', '.')
'/-[0-9]+x[0-9]+\./i' is a string.
/-[0-9]+x[0-9]+\./i is regex.
"hi".match('/hi/') // returns null
"hi".match(/hi/) // returns ["hi"]
Edit: Also, just to be clear, there's nothing wrong with your regex other than the quotes. You may want to consider using /g (i.e. /gi at the end) if you need to replace more than one match, but that's it.
I am trying to use John Gruber's URL regex in Javascript but NetBeans keeps telling me there is a syntax error and illegal errors:
var patt = "/(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])
|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]
{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|
(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|
(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:
'".,<>?«»“”‘’]))/";
Anyone know how to solve this?
As others have said, it's the double quote. But alternatively, you can just write the regexp as a literal in javascript (but then you need to escape the forward slashes in lines 1 and 3 instead).
var regexp = /\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/i;
I also moved the case-insensitive modifier to the end. Just because. (edit: Well, not just "because" - see Alan Moore's comment below)
Note: Whether you use a literal or a string, it has to be on 1 line.
put the whole expression in one line, and remove the quotes at the start and end so it looks like this var patt = /the-long-patttern/;, netbeans will still complain, but the browsers won't and thats what matters.
You should write it like this in NetBeans:
"(?i)\\b((?:[a-z][\\w-]+:(?:\\/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]"
+ "+[.][a-z]{2,4}\\/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))"
+ "+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»“”‘’]))";