I'm using a simple regex to test a string, it works just fine until I add delimiters to it. Can someone please explain why is this happening, do I have to add the delimiters in every regex?
var Cont = $("#input").val(),
cCheck = "^[0-9a-zA-Z\-\u0590-\u05FF !?.,]+$", //this one works
cCheckB = "/^[0-9a-zA-Z\-\u0590-\u05FF !?.,]+$/"; //this one doesnt
if(!Cont.match(cCheck)){
alert("bad"); return false;
}
else{
alert("good"); return false;
}
You do not quote a regexp in JavaScript when using delimiters.
var cCheckB = /^[0-9a-zA-Z\-\u0590-\u05FF !?.,]+$/;
I suspect you are trying to use a regular expression literal, which in javascript is delimited with '/'. However, if you use that you don't want the quotes around it.
cCheck = /^[0-9a-zA-Z\-\u0590-\u05FF !?.,]+$/
Related
I'm trying to validate the value of an input text field with the following code:
function onBlurTexto(value) {
var regexNIT = "([a-zA-z]|[0-9]|[&#,#.ÑñáéíóúÁÉÍÓÚ\|\s])";
regexCompilado = new RegExp(regexNIT);
if (!(regexCompilado.test(value))) {
alert("Wrong character in text :(");
return false;
} else {
return true;
}
}
But when i enter this text:
!65a
the function returns true (as you can see, the "!" character does not exist in the regular expression)
I'm not an expert in regular expressions, so i think i am missing something in the building of this reg.exp.
How can i put this regular expression to work?
Thanks in advance.
EDIT
i am so sorry ... i should remove the references to the variable "regexpValidar" before posting the issue. I modified the sample. Thanks #TecBrat
You should provide the start (^) and end ($) flags to your regex. Now you are matching 65a since you have alternate sets.
This should work /^([a-zA-z]|[0-9]|[&#,#.ÑñáéíóúÁÉÍÓÚ\|\s])+$/g
Demo: https://regex101.com/r/zo2MpN/3
RegExp.test looks for a match in the string, it doesn't verify that the whole string matches the regex. In order to do the latter, you need to add start and end anchors to your regex (i.e. '^' at the start and '$' at the end, so you have "^your regex here$").
I also just noticed that your regex is currently matching only one character. You probably want to add a '+' after the parens so that it matches one or more:
"^([a-zA-z]|[0-9]|[&#,#.ÑñáéíóúÁÉÍÓÚ\|\s])+$"
This is wrong. the variable you use doesn't has anything. Try this instead.
var regexCompilado = new RegExp(regexNIT);
I have this URL :
http://example.com/example/sample/example.jpg
I want to have this :
http:\ /\ /example.com\ /example\ /sample\ /example.jpg
I wrote this code :
function addslashes(str) {
return str.replace('/', '\/');
}
var url = http://example.com/example/sample/example.jpg
var t = addslashes(url);
alert(t);
As an alert, I still get the old URL. What's wrong with this code?
Thanks.
If you want to print \ you must escape it with another backslash.
function addslashes(str) {
return str.replace(/\//g, '\\/');
}
Also, if you want the replace function to replace all occurrences, you must pass a regex with a g modifier instead of a string. If you pass a string it will only replace the first match and then end but with the modifier it will find all matches.
try this code fiddle:
function addslashes(str) {
return str.replace(/\//g, '\\/');
}
you need to add the g to set it to global, to replace all the '/' and in the replacing string you need to add '\'.
You have to add an additinal backslash to escape it right.
With replace you would only replace the first match. You can also use Regular expression as you can see on the other posts. But you can also use it with simple split and join functions
function addslashes(url) {
url.split('/').join('\\/');
}
Demo
I'm trying to replace multiple occurrences of a string and nothing seems to be working for me. In my browser or even when testing online. Where am I going wrong?
str = '[{name}] is happy today as data-name="[{name}]" won the match today. [{name}] made 100 runs.';
str = str.replace('/[{name}]/gi','John');
console.log(str);
http://jsfiddle.net/SXTd4/
I got that example from here, and that too wont work.
You must not quote regexes, the correct notation would be:
str = str.replace(/\[{name}\]/gi,'John');
Also, you have to escape the [], because otherwise the content inside is treated as character class.
Updating your fiddle accordingly makes it work.
There are two ways declaring regexes:
// literal notation - the preferred option
var re = /regex here/;
// via constructor
var re = new Regexp('regex here');
You should not put your regex in quotes and you need to escape []
Simply use
str = str.replace(/\[{name}\]/gi,'John');
DEMO
While there are plenty of regex answers here is another way:
str = str.split('[{name}]').join('John');
The characters [ ] { } should be escaped in your regular expression.
I am trying to write some Javascript to hide some elements that contain only carriage returns. I appreciate that the correct way to solve this problem would be to stop these elements being created, but unfortunately that is not possible in this instance. I am trying to user a regular expression to search for the unwanted elements but am not having much luck. The function I have written is as follows:
function HideEmptyP()
{
var patt = (\\r)
for(var i = 0;i<desc[i].length;i++);
{
var desc[i] = document.getElementsByClassName('sitspagedesc');
var result[i] = patt.test(desc[i]);
if (result[i] == true)
{
desc[i].style.display='none';
}
else
{
alert("No Match!");
}
}
The error I'm getting in the Web Console is 'Syntax Error: Illegal Character'.
Grateful for any ideas on how to solve this.
Thanks in advance.
I am trying to write some Javascript to hide some elements that contain only carriage returns.
There's no need for a regular expression for that, just compare the element's innerHTML property to "\\r", e.g.:
if (demo[i].innerHTML === "\\r") {
// Remove it
}
But beware that some browsers may transform a single carriage return. You might want to check for "\\r", "\\n", and just a space. To do that, you might want to use a regular expression.
Your regular expression literal ((\\r)) is just completely invalid, it's worth reading up on them to learn the correct syntax. To write a regular expression literal in JavaScript, you use / as the delimiter. So: /\\r/. To test that a string contains only \r, \n, or space, you can use /^[\r\n ]+$/ (which requires there be at least one character that matches, and uses ^ to indicate start-of-string, and $ to indicate end-of-string):
if (demo[i].innerHTML.match(/^[\r\n ]+$/) {
// Remove it
}
The reason you are getting Syntax error is because the declaration
var patt = (\r)
is incorrect it should be somethign like var patt = '\r';
Also the whole for loop is wrong.
You should define demo before you start the for loop not inside it, and result need not be an array but just a normal variable
Your litteral seems odd.
Try var patt = /\r/;
var patt=/\n/gi
should work.
extra i flag to denote case insensitive.
g for global search.
matchArray becomes null for input like asklas#(((# How do I correct this behavior? I only want to allow characters and numbers..
function validateName(name) {
debug(name);
var namePat = /^(\[A-Za-z0-9]*)$/ ;
var matchArray = name.match(namePat);
if (!matchArray){
debug ("Invalid name,", name );
return false;
}
return true;
}
There is one erroneous backslash in your regex. It should be
var namePat = /^[A-Za-z0-9]*$/;
(and you don't need the capturing parentheses, either).
Not sure what you want in this case... if you want a boolean output, use .test:
namePat.test(name)
... but null will work for your test (!matchArray) just fine.
It does seem like you have a typo in your regular expression - you'll want to get rid of the backslash before the opening bracket...