Username Validation [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 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

Related

Regex for multiple spaces between words [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 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 ">'));

Javascript - Regex Does not contain some 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 4 years ago.
Improve this question
I try to regex for not contain some character.
I need to show /%7(.*?);/g which dose not contain "=".
I try to input
?!xx=1
and change to
?!( (.?)=(.?) )
But it dose not work.
Please help. Thanks.
//Here is my simple regex
reg = /%7((?!xx=1).*?);/g ;
//Here is my string
str = "%7aa; %7bb=11; %7cc=123; %7xx=1; %7yy; %7zz=2;"
//I need
%7aa; and %7yy;
Instead of using a negative lookahead, try using a ^ block:
const reg = /%7([^=;]+);/g;
The ([^=;]+) bit matches any non-=, the condition you're looking for, and non-;, the character at the end of your regex.
I left the capture group in since your question's regex also contains it.
const reg = /%7([^=;]+);/g;
const str = "%7aa; %7bb=11; %7cc=123; %7xx=1; %7yy; %7zz=2;"
const matches = str.match(reg);
console.log(matches);

Replace comparation with capital letters on 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 6 years ago.
Improve this question
I don't know how to replace with capital letters in this input text, for example I want to put on bold the letters which they are de same on the input, but if is capital letters doesn't put on bold
this is my line of code $texto_option = $texto_option.replace($(input).val(),'<b>'+$(input).val()+'</b>');
Generate a regular expression that ignores the letters' case:
var search = $(input).val();
var re = new RegExp(search, "i");
$texto_option = $texto_option.replace(re, "<b>$&</b>");
This is an easy answer. But keep in mind that your input has to be sanitized, as some characters are control characters for regular expressions too:
search = search.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");

How to allow a word that contains a banned word? [closed]

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 ' '.

Javascript regex to remove number, then a space? [closed]

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 3 years ago.
Improve this question
How would I go about removing numbers and a space from the start of a string?
For example, from '13 Adam Court, Cannock' remove '13 '.
Search for
/^[\s\d]+/
Replace with the empty string. Eg:
str = str.replace(/^[\s\d]+/, '');
This will remove digits and spaces in any order from the beginning of the string. For something that removes only a number followed by spaces, see BoltClock's answer.
str.replace(/^\d+\s+/, '');
var text = '13 Adam Court, Cannock';
var match = /\d+\s/.exec(text)[0];
text.replace(match,"");
The above solution doesn't work for me. Instead I used this regex below.
var sRegExResult = "Regex Sample99";
sRegExResult.replace(/\s|[0-9]/g, '');

Categories

Resources