/^[a-z0-9]+$/i
^ Start of string
[a-z0-9] a or b or c or ... z or 0 or 1 or ... 9
+ one or more times (change to * to allow empty string)
$ end of string
/i case-insensitive
Update (supporting universal characters)
if you need to this regexp supports universal character you can find list of unicode characters here.
for example: /^([a-zA-Z0-9\u0600-\u06FF\u0660-\u0669\u06F0-\u06F9 _.-]+)$/
this will support persian.
Answer from Greg on Stack Overflow/^[a-z0-9]+$/i
^ Start of string
[a-z0-9] a or b or c or ... z or 0 or 1 or ... 9
+ one or more times (change to * to allow empty string)
$ end of string
/i case-insensitive
Update (supporting universal characters)
if you need to this regexp supports universal character you can find list of unicode characters here.
for example: /^([a-zA-Z0-9\u0600-\u06FF\u0660-\u0669\u06F0-\u06F9 _.-]+)$/
this will support persian.
If you wanted to return a replaced result, then this would work:
var a = 'Test123*** TEST';
var b = a.replace(/[^a-z0-9]/gi, '');
console.log(b);
This would return:
Test123TEST
Note that the gi is necessary because it means global (not just on the first match), and case-insensitive, which is why I have a-z instead of a-zA-Z. And the ^ inside the brackets means "anything not in these brackets".
WARNING: Alphanumeric is great if that's exactly what you want. But if you're using this in an international market on like a person's name or geographical area, then you need to account for unicode characters, which this won't do. For instance, if you have a name like "Âlvarö", it would make it "lvar".
UPDATE: To support unicode alphanumeric, then the REGEXP could be changed to: /[^\p{L}\p{N}]/giu.
Quick way to extract alphanumeric characters from a string in Javascript - Stack Overflow
jquery - Allowing only Alphanumeric values - Stack Overflow
Check for Palindromes: Would a ``` for loop ``` help? Removing non-Alphanumeric Characters in Javascript?
javascript - Regex to remove all non alpha-numeric and replace spaces with + - Stack Overflow
Many ways to do it, basic regular expression with replace
var str = "123^&*^&*^asdasdsad";
var clean = str.replace(/[^0-9A-Z]+/gi,"");
console.log(str);
console.log(clean);
"sdfasfasdf1 yx6fg4 { df6gjn0 } yx".match(/([0-9a-zA-Z ])/g).join("")
where sdfasfasdf1 yx6fg4 { df6gjn0 } yx can be replaced by string variable. For example
var input = "hello { font-size: 14px }";
console.log(input.match(/([0-9a-zA-Z ])/g).join(""))
You can also create a custom method on string for that. Include into your project on start this
String.prototype.alphanumeric = function () {
return this.match(/([0-9a-zA-Z ])/g).join("");
}
then you can everythink in your project call just
var input = "hello { font-size: 14px }";
console.log(input.alphanumeric());
or directly
"sdfasfasdf1 yx6fg4 { df6gjn0 } yx".alphanumeric()
The better solution might be to go with regular expression based checks. Example below will limit only alphanumeric characters in the field with id text:
$('#text').keypress(function (e) {
var regex = new RegExp("^[a-zA-Z0-9]+$");
var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
if (regex.test(str)) {
return true;
}
e.preventDefault();
return false;
});
A digit in the range 1-9 followed by zero or more other digits:
^[1-9]\d*$
Ensure that a string only contains (ASCII) alphanumeric characters, underscores and spaces
^[\w ]+$
Only Alphabets no space or _
^[a-zA-Z]+$
To match a string that contains only those characters (or an empty string), try
^[a-zA-Z0-9_]*$
Some patterns that may help ..but I think you will have to modify them as per your need..I hope it help ..
This is actually fairly straightforward.
Assuming str is the string you're cleaning up:
str = str.replace(/[^a-z0-9+]+/gi, '+');
The ^ means "anything not in this list of characters". The + after the [...] group means "one or more". /gi means "replace all of these that you find, without regard to case".
So any stretch of characters that are not letters, numbers, or '+' will be converted into a single '+'.
To remove parenthesized substrings (as requested in the comments), do this replacement first:
str = str.replace(/\(.+?\)/g, '');
function replacer() {
var str = document.getElementById('before').value.
replace(/\(.+?\)/g, '').
replace(/[^a-z0-9+]+/gi, '+');
document.getElementById('after').value = str;
}
document.getElementById('replacem').onclick = replacer;
<p>Before:
<input id="before" value="Durarara!!x2 Ten" />
</p>
<p>
<input type="button" value="replace" id="replacem" />
</p>
<p>After:
<input id="after" value="" readonly />
</p>
str = str.replace(/\s+/g, '+');
str = str.replace(/[^a-zA-Z0-9+]/g, "");
- First line replaces all the spaces with + symbol
- Second line removes all the non-alphanumeric and non '+' symbols.