How to replace compounds with special characters (e.g. +⃗) - javascript

I have a string with compound characters assembled of RegEx special characters. (e.g. (⃗ and +⃗ ). Now I want to replace them with something else using javascript on nodejs.
The problem is, that the interpreter thinks the + in the compound is a special character and throws this exception: SyntaxError: Invalid regular expression: /+⃗/: Nothing to repeat
Any ideas?

You can write your regex as
var regex = /\+\u20d7/; // for +⃗

Related

Is there a way to add the ']' special character in my regex, when it normally ends my proposition?

I am trying to fit ']' into my regex that lets any string pass as long as it doesn't have a certain set of special characters. However, the IDE shows me that it ends the expression:
message: Joi.string().regex(/^[^<>#*=+^}[]+$/).required()
//current working code
message: Joi.string().regex(/^[^<>#*=+^}[]]+$/).required()
//what I am trying to add
You can escape special character using \
So you can use:
(/^[^<>#*=+^}[\]]+$/)
which will escape the first ] allowing it to be viewed as a regular symbol.
If you are new to regular expressions, try regex101.com. It does a good job of breaking them down for you.

Error Uncaught SyntaxError: Invalid regular expression: /(/: Unterminated group

I'm checking if my string has special characters to replace but for the following string I'm having the following problem
String
(Lot P Verdes)
Function
function retira_acentos(palavra) {
com_acento = 'áàãâäéèêëíìîïóòõôöúùûüçÁÀÃÂÄÉÈÊËÍÌÎÏÓÒÕÖÔÚÙÛÜÇ';
sem_acento = 'aaaaaeeeeiiiiooooouuuucAAAAAEEEEIIIIOOOOOUUUUC';
nova='';
for(i=0;i<palavra.length;i++) {
if (com_acento.search(palavra.substr(i,1))>=0) {
nova+=sem_acento.substr(com_acento.search(palavra.substr(i,1)),1);
}
else {
nova+=palavra.substr(i,1);
}
}
return nova.toUpperCase();
}
Error
line: if (com_acento.search(palavra.substr(i,1))>=0)
Uncaught SyntaxError: Invalid regular expression: /(/: Unterminated group
The problem you've stumbled across here is that String#search requires a regexp as input, you, however, seem to want to search for a string input, not a regexp. In that case, use String#indexOf instead.
Try changing these lines and see if it gives you the desired output:
if (com_acento.indexOf(palavra.substr(i,1))>=0) {
nova+=sem_acento.substr(com_acento.indexOf(palavra.substr(i,1)),1);
}
In regular expression, round parenthesis are used to define groups. In this case, the regex parser thinks that you opened a group but you forgot to close it.
You don't want to open a group, I guess. You just want to match the literal character.
To match literal characters, in javascript regex, you have two ways:
Escape the character with the backslace: \. Example: /\(/
Wrap your character in square brackets. Example: [(]
In your case, it would be preferrable to use the second approach, because it works with any character (even with the ones that don't need to be escaped) and works also with many characters.
So I advise you to change the parameter of search in this way:
search('['+palavra.substr(i,1)+']')

Javascript: regex for parsing quoted strings containing escaped quote - returns unterminated character class

Searching for a regex which parses efficiently strings containing escaped quote I ended up with the fallowing regular expression literal:
/"[^"\\]*(?:\\.[^"\\]*)*"/
It works well and fast if, for example, used to split a strings like:
var str = 'This is a block of text containing a "string with a \" (escaped quote) in it"';
str.split(/("[^"\\]*(?:\\.[^"\\]*)*")/);
Now the trouble comes in play when i try to build dynamically the regex making use of the built-in RegEx object:
/* splits by space characters and
strings containing escaped quote */
var re = new RegExp("(\\s|\"[^\"\\]*(?:\\.[^\"\\]*)*\")");
How noticeable I know that this use case requires to escape metacharacters and quotes. Nevertheless i get the fallowing errors:
Safari says
SyntaxError: Invalid regular expression: missing terminating ] for character class
Firefox:
SyntaxError: unterminated character class
By the way, the error message returned from Safari give me a little more clue making clear that the regex engine detects a missing closing square bracket, requiring the backslash character before itself to be escaped like so:
v v
var re = new RegExp("(\\s|\"[^\"\\\\]*(?:\\.[^\"\\\\]*)*\")");
but this way I realize that strings containing escaped quote are no more parsed correctly.
Any help or suggestion is really appreciated.
Taking account also of the comment above, I've examined more in depth the topic and finally, thanks to the observation pointed out by #NullUserException, I got the solution. I've realized that the regex object:
var re = new RegExp("(\\s|\"[^\"\\\\]*(?:\\.[^\"\\\\]*)*\")");
didn't work because for a mere oversight I didn't escape correctly the part which detects characters preceded by backslash (escape chars). So, in a string, the sequence \\. must be \\\\.:
var re = new RegExp("(\\s|\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\")");
Here's a simple live demonstration: http://jsfiddle.net/9ctw66pu/

Javascript Regex Problems : Nothing to repeat

replacedStr = replacedStr.replace(/&^*/g, "asdfasdf");
I need replace all with this regular expression:
/&^*/g
But it doesn't work, I can see the error messages Nothing to repeat in Chrome.
What's wrong with this regex?
The "nothing to repeat" error comes from improper escaping of metacharacters. Both ^ and * are consider special characters meaning the beginning of string anchor and * is a repetition operator. To literally match these characters, you need to properly escape them.
/&\^\*/g
If you're looking to replace those characters anywhere, consider using a character class.
/[&^*]/g
^ is a special meta charcater in regex which matches the start of the line boundary. In-order to match a literal ^ symbol, you need to escape ^ symbol in your regex.
I think you're trying to achieve something like in the below.
> 'foo&^*'.replace(/&\^\*/g, "asdfasdf")
'fooasdfasdf'
> 'foo&^^'.replace(/&\^*/g, "asdfasdf")
'fooasdfasdf'

understanding regular expression for detecting string

I encountered this regular expression that detects string literal of Unicode characters in JavaScript.
'"'("\\x"[a-fA-F0-9]{2}|"\\u"[a-fA-F0-9]{4}|"\\"[^xu]|[^"\n\\])*'"'
but I couldn't understand the role and need of
"\\x"[a-fA-F0-9]{2}
"\\"[^xu]|[^"\n\\]
My guess about 1) is that it is detecting control characters.
"\\x"[a-fA-F0-9]{2}
This is a literal \x followed by two characters from the hex-digit group.
This matches the shorter-form character escapes for the code points 0–255, \x00–\xFF. These are valid in JavaScript string literals but they aren't in JSON, where you have to use \u0000–\u00FF instead.
"\\"[^xu]|[^"{esc}\n]
This matches one of:
backslash followed by one more character, except for x or u. The valid cases for \xNN and \uNNNN were picked up in the previous |-separated clauses, so what this does is avoid matching invalid syntax like \uqX.
anything else, except for the " or newline. It is probably also supposed to be excluding other escape characters, which I'm guessing is what {esc} means. That isn't part of the normal regex syntax, but it may be some extended syntax or templating over the top of regex. Otherwise, [^"{esc}\n] would mean just any character except ", {, e, s, c, } or newline, which would be wrong.
Notably, the last clause, that picks up ‘anything else’, doesn't exclude \ itself, so you can still have \uqX in your string and get a match even though that is invalid in both JSON and JavaScript.

Categories

Resources