how to make regular expression match only Cyrillic bulgarian letters - javascript

Hello I want to replace all the letters from bylgarian alphabet with empty string
I've seen this link
How to match Cyrillic characters with a regular expression
but it doesn't work for me
Here is what I've tried
1. var newstr = strInput.replace(/[\p{IsCyrillic}]/gi, '');
doesn't work!
2. var newstr = strInput.replace(/[\p{Letter}]/gi, '');
also nothing
thanks for help;

Javascript doesn't support Unicode classes of the form \p{IsCyrillic}.
But, assuming the characters you want to replace are in the Unicode Cyrillic range 0400 - 04FF, you could use:
newstr = strInput.replace( /[\u0400-\u04FF]/gi, '' );
For example:
var strInput = 'уфхцчшщъhelloЁЂЃЄрстыьэю',
newstr = strInput.replace( /[\u0400-\u04FF]/gi, '' );
console.log( newstr ); // 'hello'

I think that JavaScript RegEx does not support this syntax.
May be this will help?
XRegExp

Another way:
Pattern.compile("[А-я]+", Pattern.UNICODE_CHARACTER_CLASS).matcher(strInput ).replaceAll("") ;
Where [А-я]+ is your alphabet.

Related

JS Regex for a string contains fixed number of letters

Let's say I need to have minimum 5 letters in a string not requiring that they are subsequent. The regex below checks subsequent letters
[A-Za-z]{5,}
So, "aaaaa" -- true, but "aaa1aa" -- false.
What is the regex to leave the sequence condition, that both of the strings above would pass as true.
You could remove all non-letter chars with .replace(/[^A-Za-z]+/g, '') and then run the regex:
var strs = ["aaaaa", "aaa1aa"];
var val_rx = /[a-zA-Z]{5,}/;
for (var s of strs) {
console.log( val_rx.test(s.replace(/[^A-Za-z]+/g, '')) );
}
Else, you may also use a one step solution like
var strs = ["aaaaa", "aaa1aa"];
var val_rx = /(?:[^a-zA-Z]*[a-zA-Z]){5,}/;
for (var s of strs) {
console.log( s, "=>", val_rx.test(s) );
}
See this second regex demo online. (?:[^a-zA-Z]*[a-zA-Z]){5,} matches 5 or more consecutive occurrences of 0 or more non-letter chars ([^a-zA-Z]*) followed with a letter char.
Allow non-letter characters between the letters:
(?:[A-Za-z][^A-Za-z]*){5,}
If you have to use a regular expression only, here's one somewhat ugly option:
const check = str => /^(.*[A-Za-z].*){5}/.test(str);
console.log(check("aaaaa"));
console.log(check("aa1aaa"));
console.log(check("aa1aa"));
w means alphanumeric in regex,
it will be ok : \w{5,}
[a-zA-Z0-9]{5,}
Just like this? Or do you mean it needs to be a regex that ignores digits? Because the above would match aaaa1 as well.

Add variable into string

All I need to do here is to add a variable before each specific string.
Example:
var exampleString = "blabla:test abcde 123test:123";
var formattedString = "el.blabla:test abcde el.123test:123";
As you can see, when I have something like "XXX:XXX", I need to add a variable before it.
I have the Regex to find "XXX:"
var regex = new RegExp(/\w+([aA-zZ]:)/g)
But when I try to replace it, it replaces all instead of adding the variable "el."
var exampleString = "blabla:test abcde 123test:123";
var formattedString = exampleString.replace(new RegExp(/\w+([aA-zZ]:)/g), 'el.');
// formattedString is now "el.test abcde el.123"
// Instead of "el.blabla:test abcde el.123test:123"
Could anyone makes this work ? Thanks :)
Source: Javascript Regex: How to put a variable inside a regular expression?
var exampleString = "blabla:test abcde 123test:123";
var formattedString = exampleString.replace(/\w*:\w*/gi, 'el.$&');
console.log(formattedString);
Regex use and Explanation Here https://regex101.com/r/U2KeXi/3
Sample Fiddle here https://jsfiddle.net/a8wyLb0g/2/
You need to use ^ to match only at the beginning. And remove the g modifier, since you only want to replace once, not every time.
There's also no reason to use new RegExp(), just use a RegExp literal.
In the replacement string, you need to use $& to copy the original string into the replacement.
var exampleString = "blabla:test abcde 123test:123";
var formattedString = exampleString.replace(/^\w+[a-z]:/i, 'el.$&');
console.log(formattedString);
Also, the proper way to match all letters in either case is with [A-Za-z], not [aA-zZ], or use the i modifier to make the regexp case-insensitive. Your regexp matches all characters in the range A-z, which includes lots of punctuation characters that are between the uppercase letters and lowercase letters in the ASCII code.
Just use this
exampleString.replace(/(\w*):(\w*)/gi, 'el.$1:$2');
REGEXP explanation :
capturing group (\w*) is for capturing any alphabets in any number of occurance,
$1 and $2 specifies the first and second capturing group.
You should use a function like insertAt instead replace, see following example:
String.prototype.insertAt=function(index, string) {
return this.substr(0, index) + string + this.substr(index);
}
var exampleString = "blabla:test abcde 123test:123";
var regex = new RegExp(/\w+([aA-zZ]:)/g)
var formattedString = exampleString;
while ( (result = regex.exec(exampleString)) ) {
formattedString = formattedString.insertAt(result.index, "el.");
}
console.log(formattedString);
I hope it helps you, bye.

How do I replace all spaces, commas and periods in a variable using javascript

I have tried var res = str.replace(/ |,|.|/g, ""); and var res = str.replace(/ |,|.|/gi, "");. What am I missing here?
var str = "Text with comma, space, and period.";
var res = str.replace(/ |,|.|/g, "");
document.write(res);
If you just want to delete all spaces, commas and periods you can do it like that:
var res = str.replace(/[ ,.]/g, "");
You can also use the | operator, but in that case you have to escape the period, because a plain period will match with any character. As a general remark, if in a regular expression you have multiple alternatives with | that all consist of a single character, it is preferable to use a set with [...].
You need to escape dot \.:
"Text with comma, space and period.".replace(/ |,|\.|/g, "")
You can use these lines:
str = str.replace(/[ ,.]/g,'');
Plus i have added a fiddle for this at Fiddle

How do I test for no whitespaces in a string, using regex?

How do I test for no whitespaces in a string, using regex?
I'm using JQuery
var postcode = $(this), val = postcode.val();
if(val.test(NO WHITESPACE)){
...
}
Any help is appreciated, Thanks
if (/\s/.test(string)) alert("OH NO THERE IS FILTHY WHITESPACE IN THAT STRING");
The "\s" ... uhh, thing, in a regex means "any whitespace character". Specifically, it means the same as this:
[ \f\n\r\t\v\u00A0\u2028\u2029]
which is to say, space, form feed, line feed, carriage return, tab, vertical tab, and some space-like characters from extended Latin and Unicode.
If it's only spaces, you don't need a RegExp: no spaces = val.indexOf(' ') < 0
Just use:
if(!val.match(/\s/)) {
...
}
Use the following code this would replace all the blank spaces..
string.replace(/^\s+|\s+$/g,'')
In fact it would remove even newline characters
The spaces are actually removed by \s
var valid = !/\s/.test('IHaveNoWhiteSpace')
valid ; //# => true
var valid = !/\s/.test('I Have WhiteSpace')
valid ; //# => false
var isValidPostCode = /^[a-z]{2}\d{3}[a-z]{2}$/i.test('TN809EX');
isValidPostCode ; //# => false

Regular Expression: Any character that is not a letter or number

I need a regular expression that will match any character that is not a letter or a number. Once found I want to replace it with a blank space.
To match anything other than letter or number you could try this:
[^a-zA-Z0-9]
And to replace:
var str = 'dfj,dsf7lfsd .sdklfj';
str = str.replace(/[^A-Za-z0-9]/g, ' ');
This regular expression matches anything that isn't a letter, digit, or an underscore (_) character.
\W
For example in JavaScript:
"(,,#,£,() asdf 345345".replace(/\W/g, ' '); // Output: " asdf 345345"
You are looking for:
var yourVar = '1324567890abc§$)%';
yourVar = yourVar.replace(/[^a-zA-Z0-9]/g, ' ');
This replaces all non-alphanumeric characters with a space.
The "g" on the end replaces all occurrences.
Instead of specifying a-z (lowercase) and A-Z (uppercase) you can also use the in-case-sensitive option: /[^a-z0-9]/gi.
This is way way too late, but since there is no accepted answer I'd like to provide what I think is the simplest one: \D - matches all non digit characters.
var x = "123 235-25%";
x.replace(/\D/g, '');
Results in x: "12323525"
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
Match letters only /[A-Z]/ig
Match anything not letters /[^A-Z]/ig
Match number only /[0-9]/g or /\d+/g
Match anything not number /[^0-9]/g or /\D+/g
Match anything not number or letter /[^A-Z0-9]/ig
There are other possible patterns
try doing str.replace(/[^\w]/);
It will replace all the non-alphabets and numbers from your string!
Edit 1: str.replace(/[^\w]/g, ' ')
Just for others to see:
someString.replaceAll("([^\\p{L}\\p{N}])", " ");
will remove any non-letter and non-number unicode characters.
Source
To match anything other than letter or number or letter with diacritics like é you could try this:
[^\wÀ-úÀ-ÿ]
And to replace:
var str = 'dfj,dsf7é#lfsd .sdklfàj1';
str = str.replace(/[^\wÀ-úÀ-ÿ]/g, '_');
Inspired by the top post with support for diacritics
source
Have you tried str = str.replace(/\W|_/g,''); it will return a string without any character and you can specify if any especial character after the pipe bar | to catch them as well.
var str = "1324567890abc§$)% John Doe #$#'.replace(/\W|_/g, ''); it will return str = 1324567890abcJohnDoe
or look for digits and letters and replace them for empty string (""):
var str = "1324567890abc§$)% John Doe #$#".replace(/\w|_/g, ''); it will return str = '§$)% #$#';
Working with unicode, best for me:
text.replace(/[^\p{L}\p{N}]+/gu, ' ');

Categories

Resources