Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
Why do I need to use $ sign in var reg and what is the meaning of this line. I am new to programming so please help.
<head>
<script type="text/javascript">
function myPopup2(elem, mg) {
var reg = /^[0-9]+$/;
//alert(reg);
//exit();
if (elem.value.length == 0)
{
alert(mg);
elem.focus();
return false;
}
if (elem.value.match(reg))
{
return true;
}
else
{
alert("this is not a number");
elem.focus();
return false;
}
}
</script>
</head>
<body>
<form>
<input type="text" id="name">
<input type="button" onClick="myPopup2(document.getElementById('name'), 'pleas enter a value')" value="POP2!">
</form>
</body>
$ says the End of Line
just have a look here regexper
/^[0-9]+$/ :
^ - matches only from the beginning of string
$ - matches exactly to the end of string
[] - set of characters
0-9 - 0123456789
+ - one or more occurrence of that which it follows
I highly recommend this site for anything to do with regular expressions: http://www.regular-expressions.info/
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 2 months ago.
Improve this question
I need a regex that detects words inside a string. My regex works fine when the class consists of a single word, but not for multiple words.
function repl(string){
return string.replace(/<span class="[a-z\s\-\b \b]*">/ , (n)=>{
return <Symbol/>
}
}
Works:
repl('<span class='hi'>') /==> <Symbol/>
Does not work:
repl('<span class='hi hi'>')
You can simplify your expression to: /<span class=["'][a-z_\-][\w\s\-]*["']>/i
Make sure your HTML element attributes are double-quoted.
Note: Please be aware that the closing </span> tag is mandatory and should never be ommited.
const repl = (str) =>
str.replace(/<span class=["'][a-z_\-][\w\-\s]*["']>/i, () => '<Symbol />');
console.log(repl('<span class="hi">'));
console.log(repl("<span class='hi hi'>"));
console.log(repl('<span class="one two three ">'));
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Regex pattern for following example test test123 .first string between A-za-z and second string should be A-Za-z0-9
examples:
hello world123 (true)
123 hello123 (false)
You can use like ^[a-zA-Z]+[ ][a-zA-Z0-9]+$
function CheckValidation() {
var str = document.getElementById("txtInput").value;
var pattern = new RegExp("^[a-zA-Z]+[ ][a-zA-Z0-9]+$");
var res = pattern.test(str);
document.getElementById("para").innerHTML = res;
}
<input type="text" id="txtInput" />
<button onclick="CheckValidation()">Check</button>
<p id="para"></p>
Used the below and working fine
var pattern = new RegExp("^[a-zA-Z]+[ a-zA-Z0-9]+$");
var res = pattern.test(str);
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 6 years ago.
Improve this question
I am creating a filtering function for my form's username input to permit the use of specific banned words.
I am currently using:
var forbiddenWords = ["c*m", "blabla", "blablabla"];
// Check for forbidden words
function isForbiddenWord(value) {
for (var i = 0; i < forbiddenWords.length; i++) {
var rgx = new RegExp(forbiddenWords[i], 'gi');
if (rgx.test(value)) {
forbiddenWord = forbiddenWords[i];
return true;
}
}
return false;
};
The first word in the array, "c*m", is for obvious reasons a banned word. If for example somebody types in the username "eat_a_c*mshot" I want it banned. If somebody else types in: incumbent_king, encumbrance, accumulate_wealth, cumbersome, im_a_scum, circumvent, sweet_cucumber and so on, I want these words to be allowed.
Is there a way to identify if such words are used and permit them, like with a regular expression or so, or I am asking too much?
You might want to try the \b delimiter - as in /\bc*m|\bc*m\b|c*m\b/. This is as good as you can get with regular expressions. As MikeC said, Natural language processing is a huge field.
Postscript: on closer inspection _ is actually a word character, so in order for the \b approach to work, you'd need to replace '_' 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
function validate(username) {
var reg = /^\w+([-+.]\w+)*#\w+([-.]\w+)*$/;
if(reg.test(username)) {
alert("is correct");
return true;
}
else {
return false;
}
}
Pattern requires an #
This pattern ^\w+([-+.]\w+)*#\w+([-.]\w+)*$ requires an # in your input.
It matches a#a but not someusername.
If you want to build a username regex, I suggest you can with something simple like:
^[-.\w]{2,20}$
and tweak from there.
The ^ anchor asserts that we are at the beginning of the string
[-.\w] matches one word character (letters, digits, underscores), dash or period
{2,20} matches two to 20 of these characters
The $ anchor asserts that we are at the end of the string
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i have a problem creating a reg expression.
E.g.: i have the following text "This} is} a} test}"
Now i want to replace every } including any preceded character.
Is this possible in java script with Regex?
The result of this text should be.
"Thi i tes"
How about:
str.replace(/.}/g, '')
Try This,
var st = "This} is} a} test}";
var rep = st.replace(/.\}/g, "");
console.log(rep);
<script type="text/javascript">
var test = "This} is} a} test}";
var regexp = new RegExp(".}","g");
test = test.replace(regexp, "");
alert(test); //"Thi i tes"
</script>