Can anyone explain this bug for me , what we have here is :
if(statements[bracket].firsthalf.search(math_operators[j])!=-1)
where statements[bracket].firsthalf = "2*a" , math_operators[j]="*" , the console shows the following error:
Uncaught SyntaxError: Invalid regular expression: /*/: Nothing to
repeat
any idea why would it show such error ?
Use indexOf, not search. indexOf looks for literal strings, search is for matching a regular expression. In regular expressions, most punctuation characters have special meanings and need to be escaped if you want to find them literally, which is why you're getting errors.
Search need a RegularExpression as argument.
* is used to say 0 or more of the previous expression.
Like [0-9]* = 0 or more digits.
To use * as a character you have to escape it :
\*
You have to write the search part as a regular expression.
2*a".search(*) is non sense, because it doesn't search the character (*) but 0 or more time nothing because there is nothing before the *.
It's the same thing for the + that is protected character too.
You should use another function than search or write your request in a RegularExpression compliant manner like :
search([\*|\+|\-|\/])
Related
I'm working on a regular expression that validates the pattern from A01 to A99.
I came out to this solution:
^A(?(?=0)0[1-9]|[0-9][0-9])$
Then, when I'm trying to implement it into JS I get an error that says
Uncaught SyntaxError: Invalid regular expression: /^A(?(?=0)0[1-9]|[0-9][0-9])$/: Invalid group
at new RegExp (<anonymous>)
at window.onload ((index):34)
I have tried doing:
new RegExp('^A(?(?=0)0[1-9]|[0-9][0-9])$');
Or
/^A(?(?=0)0[1-9]|[0-9][0-9])$/
But I get the same error with both of those. I know that is because of the conditional check inside de Regular Expression. Is there a way I can implement this pattern inside Js?
It is working on regex101: https://regex101.com/r/g0Qfac/1
Any ideas?
Thanks!
JavaScript regex does not support conditional constructs.
Use
^A(?:0[1-9]|[1-9][0-9])$
Or
^A(?:0[1-9]|[1-9]\d)$
Details
^ - start of string
A - a letter A
(?:0[1-9]|[1-9]\d) - either 0 followed with a digit from 1 to 9 or a digit from 1 to 9 followed with any single digit
$ - end of string.
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)+']')
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'
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 +⃗
I am able to search a string using str.search("me"); and str.search("=");
But when i serach for str.search("?");
I get the error Unexpected Quantifier.
Why is that?
How can i search for "?" using something other than a regular expression?
"?" is a special character in a regular expression (one of the "quantifiers") which means "match the preceding zero or one times". It lead to the error in this case because it was preceded by nothing. However, "a?" wouldn't have thrown an exception, but would match "b" so this is an important thing to look out for.
If using String.search, which takes a regular expression (as karim79 points out, it isn't the only way), use "[?]" or "\\?" or /\?/. These forms will prevent the "?" from being treated as a special regular expression construct.
Happy coding.
alert(str.indexOf("?")); // returns position as integer if present, -1 otherwise
Typical usage scenario is:
if(str.indexOf("?") !== -1) {
// present
}
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/indexOf
Try this:
alert(str.search("\\?"));
It is quantifier and you can`t directly use it. From javascriptkit.com
? is short for {0,1}. Matches zero or one time.
and MSDN
? Matches the preceding character or subexpression zero or one
time. For example, 'do(es)?' matches the "do" in "do" or "does". ? is
equivalent to {0,1}
This is the right usage:
str.search("\\?")