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 \$
Related
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 3 years ago.
Improve this question
I want to replace the "3" into "5". When it is using static it's working fine but when I use it through variable var allvar= '"3"'; it's not working fine.
Here is the jsfiddle link
new RegExp( /[allvar]+/g ); will construct a regular expression matching all uninterrupted sequences of one or more characters from the set a, l, v, a, r.
To construct a regular expression from a variable, you can do this:
new RegExp(allvar, 'g')
It would also be good to escape characters with special meaning to RegExp, unless you intend for allvar to contain regexp source. Unfortunately, RegExp.escape is still not in the language, so one would use a workaround.
new RegExp(escapeRegExp(allvar), 'g')
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);
}
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.
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
How to get this string to give me the exact matching result
session[username_or_email]
the expression
<html>
<body>
<script type="text/javascript">
var str="session[username_or_email]";
var patt1=/ID|un|name|login_username|userid|username|user|Email|uname|usr|log|email|mail|nick|CUST|account|wpName1|textbox|pw|session[username_or_email]/i;
document.write(str.match(patt1));
</script>
</body>
</html>
the result now is username
Thankyou.
Just escape the [ with \[ so it isn't treated as a special regex control character, but just a normal character to search for.
var str="session[username_or_email]";
var patt1=/ID|un|name|login_username|userid|username|user|Email|uname|usr|log|email|mail|nick|CUST|account|wpName1|textbox|pw|session\[username_or_email]/i;
document.write(str.match(patt1));
You can see it work here: http://jsfiddle.net/jfriend00/CuWKV/
You don't actually have to escape the ] because it is only an expected regex control character when a [ has come before it though it does not harm to escape it also.
Please note the escaped \[ - I also moved the wanted string to the beginning since it contains username too - this may or may not be what you need, but you should consider it.
var str="session[username_or_email]";
var patt1=/session\[username_or_email\]|ID|un|name|login_username|userid|username|user|Email|uname|usr|log|email|mail|nick|CUST|account|wpName1|textbox|pw/i;
document.write(str.match(patt1));