regular expression with limited special characters in jquery [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I would like to all alphanumeric data +
only these following 4 special character are allowed.
' (single quote)
- (hyphen)
. (dot)
single space
I tried this :
var userinput = $(this).val();
var pattern = [A-Za-z0-9_~\-!##\$%\^&\*\(\)]+$
if(!pattern.test(userinput))
{
alert('not a valid');
}​
but it is not working.

First, you need to enclose the string in / to have it interpreted as a regex:
var pattern = /[A-Za-z0-9_~\-!##\$%\^&\*\(\)]+$/;
Then, you have to remove some unallowed characters (that regex is matching more than you specified):
var pattern = /^[A-Za-z0-9 '.-]+$/;
The second one is what you need. Complete code:
var userinput = $(this).val();
var pattern = /^[A-Za-z0-9 '.-]+$/;
if(!pattern.test(userinput))
{
alert('not a valid');
}​
Besides, check what this points to.

"Not working" is not a helpful description of your problem.
I'd suggest this regular expresion:
^[a-zA-Z0-9\'\-\. ]+$

Related

regex match first occurrence of a single character [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'll make this extremely simple
I have a function that comes across a string as such by design: water 'thing one'
I need to split this string by the first space, since there are spaces elsewhere.
I've tried many regex expressions like / .*? /, but they can only match two consecutive spaces.
How do I do this?
Thanks in advance.
If you just want the portion of the string before and after the first space, you could use regex replacement here:
var input = "water 'thing one'";
var first = input.replace(/[ ].*$/, "");
var second = input.replace(/^\S*[ ]/, "");
console.log("first part: " + first);
console.log("second part: " + second);
You can capture them using String#match with this regex:
/(.*?) (.*)/
const text = "water 'thing one'";
const [, first, second] = text.match(/(.*?) (.*)/);
console.log('first:', first);
console.log('second:', second);

How to show first 2 characters and replace all the last by * [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm wondering how to show the first two characters and replace all last character of a string by symbol *.
Ex: 121,121,121 -> 12x,xxx,xxx .
Thanks
I love using regex when it comes to replace string according to some pattern.
var p = '121,121,121';
var regex = /(?<=.{2})([0-9])/gm;
console.log(p.replace(regex, 'x'));
You can use substring and regular expression. See the sample below.
var str = "121,121,121";
var res = str.substring(0, 2) + '' + str.substring(2, str.length).replace(/[0-9]/g,"x");
alert(res);
Just use substring and replace with a simple regex (to single out digits and keep commas and other punctuation):
const str = "121,121,121";
const obfuscated = `${str.substring(0, 2)}${str.substring(2).replace(/\d/g, "*")}`;
console.log(obfuscated);

Regex Only one special character at a time from allowed special characters Javascript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I want to allow 3 characters 1st Underscore (_) , 2nd hyphen (-) 3rd Dot (.)
but i want to put conditions that only one character is allowed at a time from all of them, and also only one time.
e.g
allowed usernames = abc.def , abc-def , abc_def
not allowed usernames = abc.de.f alert here(only one special character is allowed in a username)
not allowed usernames = abc.de-f , abc.de_f , ab-cd_ef
What should i do.
Try /^[a-z]*[-._]?[a-z]*$/
var tests = ['abc.def', 'abc-def', 'abc_def', 'abc.de.f','abc.de-f' , 'abc.de_f', 'ab-cd_ef'];
$.each(tests, function(a,b) {
$('body').append('<div>' + b + ' = ' + regIt(b) + '</div>');
});
function regIt(str) {
return /^[a-z]*[-._]?[a-z]*$/.test(str);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
^[a-z]*([-._]?)(?:[a-z]|\1)*$
This regex will match letters until it reaches the end of the string. If it reaches a symbol (- . or _) it will store that symbol as group 1, and keep matching for letters or that same symbol until the end of the string.
The following are matching examples:
an empty string
_something
something-something
foo_bar_baz
foo.
And here are some invalid strings:
my_file.txt
alpha.bravo_charlie
not-working_

Regx for correct filename 1201_17-11-2015.zip [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need to check whether a given file name is in the correct format or not.
Means:
first four numbers_two numbers-two numbers-4 numbers.zip
for that I need a regular expression.
Example file name is (1201_17-11-2015.zip) in javascript
var re = new RegExp('^\d{4}_\d{2}-\d{2}-\d{4}.zip$');
if (filename.match(re)) {
//successful match
}
You regexp could look something like this:
^\d{4}_\d\d-\d\d-\d{4}.zip$
^ is the beginning of your pattern
\d means any number
{n} means that the last pattern has to exist n-time
$ is the end of your pattern
On this site you can start learning how to use regular expression
Here you can test if your own regular expression is working...
I find it best to test scenarios at RegExr. None the less, what you're asking for is basic:
var result = "1201_17-11-2015.zip".match(/\d{4}_\d{2}-\d{2}-\d{4}\.zip/)
if (result == null) {
console.warn("Unable to find a match");
} else {
console.log("Found match: %k", result);
}

javascript expressions names [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am a complete beginner to javascript and I have several things I need to correct on a form in order for it to work. I have to make sure it doesn't reject any valid names (names with accents, hyphens, names with spaces between them). At the moment my regular expression is -
var alphabetic = /^[a-zA-Z]+$/;
if ((alphabetic.test(fname)== false) || (alphabetic.test(lname)== false))
{
alertmsg = alertmsg + "Name should be in alphabets:" + "\n";
}
If someone could point me in the right direction, I would be very grateful
Try this regex :
var alphabetic = /^[a-zàâçéèêëîïôûùüÿñ-\s]+$/i
As Philippe recommended, if you would like to accept languages/alphabets other than English, I would consider more carefully which letters to include. [a-zA-Z] does not seem to recognize letters other than strictly 'A' to 'Z' in my testing.

Categories