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)+']')
Related
In an example piece of code, I stumbled upon this line:
// Change the string into lower case and remove all non-alphanumeric characters
var cstr = str_entry.toLowerCase().replace(/[^a-zA-Z0-9]+/g,'');
I think I understand that the /g inside the parameter makes everything in between the // become empty strings (''). Am I correct?
What does the ^ part of the parameter do? What does everything inside the [ ] brackets mean?
The first parameter of the replace function is a regular expression, which is a way of determining if a string matches a complex pattern.
The /g parameter means 'global', so if two parts of the str_entry string match, they will both replaced with an empty string, instead of just the first one.
The ^ within [] means 'not', so it's saying 'check if the string is not a-zA-Z0-9'.
More simply, the regular expression is identifying any non-alphanumeric characters in your string. Using it with replace(..., '') will remove those characters.
Take a look at Regex101 for more information about how regular expressions work. You can punch in your regular expression and it will tell you what each part of it does.
I'm trying to figure out the following regular expression:
/^[0-9]{2}-[0-9]{2,3}[a-zA-z]{0,1}/g
In my example.
The following should pass: 00-45, 00-333, 33-333a, 55-34a
The following should fail: 33-3333, 22-22dd, 22-2233
Here is my screenshot:
But the once that should fail arent failing. In my javascript code I just do a test:
var regExp = new RegExp(exp);
if(regExp.test(test1))
alert('pass');
else
alert('fail');
Is there a way for the regular expression to test the entire string? Example 33-3333 passes because of 33-333, but since there is another 3 I would like it to fail since the fourth 3 would be tested against the character rule?
You are missing end anchor $ in your input
A-z inside character class will match unwanted characters as well, you actually need A-Z
{0,1} can be shortened to ?
Try this regex:
/^[0-9]{2}-[0-9]{2,3}[a-zA-Z]?$/
RegEx Demo
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'
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([\*|\+|\-|\/])
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 +⃗