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 have the below wild card search which looks for the following conditions.
$.validator.addMethod("FirstName", function (value, element) {
return this.optional(element) || /^[a-zA-Z''-'\s]{3,20}[\%\*]{0,1}$/i.test(value);
}, "For wild card search minimum of 3 characters should be entered.");
$("#frmJscsSearch").validate({
rules : {
FirstName : "required FirstName"
},
});
if user enter 2 characters, its valid
If user enter 3 characters, its valid
How do I validate if user enter 2 characters WITH * then its invalid?
See if this regex does what you want (the idea is to use alternation |):
/(^[a-z '\-]+$)|(^[a-z '-]{3,}[\%\*]$)/i
Your original regex was a little messy also, so I've edited it.
You can also try checking for the wrong input instead of the right one:
var re = /([^a-z '\-*%])|(^[\w\W]{0,2}[\*\%]&)|(^[ \-*%'])|([ \-]$)|([ \-']{2,})/i
!(re.test(value))
That way you can check for more specific conditions easier. The regexp above describes the following rules:
value should contain only a-z '-*% characters;
* or % should be the last symbol (if used) preceded by at least three other characters;
value can't start with -'*%;
value can't end with -;
value can't contain multiple -' in a row.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 months ago.
Improve this question
I have a long document with some headings I want to replace in a operation.
The headings have the following structure (there are two nouns, both in with a uppercase first character, separated with a whitespace, also the time is dynamic):
let string = 'Firstname Lastname [00:01:02]';
I want to insert some characters at the front and the end of this string, but want to keep the content.
So the desired output should be something like:
let string = '{Firstname Lastname [00:01:02]}:';
I tried a little bit around with RegEx and can catch the time with the following pattern:
\[[0-9]{2}:[0-9]{2}:[0-9]{2}
I figured it out by using captures in my RegEx.
/(\b[A-Z][a-z]* [A-Z][a-z]*( [A-Z])?\b\s\[[0-9]{2}:[0-9]{2}:[0-9]{2}\])/g
This RegEx captures the pattern of my headings into group one. In a replace operation I can then insert the desired content.
string.replace(regex, '{$1}:')
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 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
Improve this question
I have a string like:
def definition():
I want to change word def (for example), every instance of word def but not the "def"s that are part of other words
like this
console.log("def definition():".specialReplace("def", "abc"));
and result should be
abc definition():
not
abc abcinition():
Use String#replace or String#replaceAll with a regular expression:
const specialReplace = (str) => str.replaceAll(/\bdef\b/g, 'abc')
console.log(specialReplace("def definition")) // abc definition
console.log(specialReplace("def definition def")) // abc definition abc
In the regular expression, \b is a boundary type assertion that matches any word boundary, such as between a letter and a space.
Note that the same sequence \b is also used inside character class regular expression positions ([\b]), to match the backspace character.
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_
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 would like to know how I set a regex pattern for aphanumeric and dollar sign.
Except for dollar sign, it does not accept any other special characters.
Here are examples...
The pattern should be okay with ....
hahah
hohho
hihihi
$hahah
hahah I will get $100 for this
The pattern should be sad with ....
hi James.
#fdasfdas
run!
Any idea?
so you want it to require a '$' symbol somewhere in the string? – yes.
Do you want to allow spaces also? - yes
please add more details, unless the below answer is what you are looking for. Currently this isn't a clear question. – sorry I just got back to my machine.
public static bool IsAlphanumericCharactersAndDollarSign(string str)
{
if (str == null) return false;
Regex rg = new Regex(#"/[a-zA-z0-9\s\$]*/");
return rg.IsMatch(str);
}
Pattern for this: /[a-zA-z0-9\s\$]*/ match alphanumeric, spaces and $ sign 0 or more times
This is PCRE compliant, but in perl for example you need to escape the $ with \$
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.