using regex in javascript to check if foreign characters [closed] - javascript

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 5 years ago.
Improve this question
I'm trying to create a simple function that checks if there are foreign characters in my string. Essentially, this is what I'm trying to get:
var str_1 = "отправка.py" // should return true
var str_2 = "bla.py" // should return false
var str_3 = "bla23.py" // should return false
var str_4 = "bla23_.py" // should return false
I want to make sure that there aren't any foreign characters, while still making sure that I allow everything else (characters like "_", "-", ...).
I'm just trying to avoid the Russian, Mandarin, etc.. alphabets.

You are looking only for ASCII. So something like:
!/^[\x00-\x7F]*$/.test('отправка.py'); // => true
!/^[\x00-\x7F]*$/.test('bla.py'); // => false

Code
See regex in use here
[^ -~]
Alternatively: [^\x00-\x7F] (which seems to have already been posted by #BlakeSimpson)
Usage
const r = /[^ -~]/
const a = [
"отправка.py",
"bla.py",
"bla23.py",
"bla23_.py"
]
a.forEach(function(s) {
if(r.exec(s) !== null) {
console.log(s)
}
})
Explanation
[^ -~] Matches everything that's not from the space character (DEC 32) to the tilde ~ symbol (DEC 126) - which are all the visible ASCII characters.
Also, note that I don't use the g modifier. This is intentional as the OP is only asking to check whether or not there are foreign characters in the string. That means that so long as 1 character exists in the string that meet those requirements it should be matched (no need to match more than one).

Related

First name only Regular Expression [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 5 months ago.
Improve this question
I'm trying to come up with a regular expression for a first name only that should not contain the following:
Symbols, numbers, unusual capitalization or punctuation.
Characters from multiple languages – meaning one cannot have the Latin/roman alphabet mixed. with Arabic script or Katakana Japanese script.
I tried this which I found on this platform '/^(?=.{1,50}$)[a-z]+(?:['_.\s][a-z]+)*$/i'
But it accepts DaVid, DaaaaaaaaaaVid when it should fail them.
About the capital, adding [A-Z] and removing the i (case insensitive option) makes it:
const regex = /^(?=.{1,50}$)[A-Z][a-z]+(?:['_.\s][a-z]+)*$/
const names = [
"DaVid", // Rejected
"DaaaaaaaaaaaVid", // Rejected
"David", // Accepted
"Ann", // Accepted
"Soooooooophie", // Accepted
]
names.forEach((name) => console.log(regex.exec(name) ? "Accepted" : "Rejected"))
But for letters repeating more than 2 times, I don't know how that would be possible in just one regular expression.
While this one detects it:
const regex = /([a-z])\1{2,}/
const names = [
"DaVid", // Ok
"DaaaaaaaaaaaVid", // Repeating char detected
"David", // Ok
"Ann", // Ok
"Soooooooophie", // Repeating char detected
]
names.forEach((name) => console.log(regex.exec(name) ? "Repeating char detected" : "Ok"))

Find and replace # mentions using 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 2 years ago.
Improve this question
I'm trying to parse strings to find and replace # mentions.
Here is a sample string:
Hi #jordan123 and #jordan have a good day
I want to find and replace #jordan with #jordananderson without modifying #jordan123
I used this regex to find a list of all of the mentions in the string:
let string = 'Hi #jordan123 and #jordan have a good day'
let result = string.match(/\B\#\w\w+\b/g);
that returns:
['#jordan123', '#jordan']
But I can't figure out how to continue and complete the replacement.
Valid characters for the username are alphanumeric and always start with the # symbol.
So it also needs to work for strings like this:
Hi #jordan123 and #jordan!! have a good day
And this
Hi #jordan123! and !#jordan/|:!! have a good day
My goal is to write a function like this:
replaceUsername(string, oldUsername, newUsername)
If I understand your question correctly, you need \b, which matches a word boundary:
The regex: #jordan\b will match:
Hi #jordan123 and #jordan!! have a good day
Hi #jordan123! and !#jordan/|:!! have a good day
To build this regex, just build it like a string; don't forget to sanitize the input if it's from the user.
var reg = new RegExp("#" + toReplace + "\\b")
In general if you have one string of a found value, and a larger string with many values, including the found value, you can use methods such as split, replace, indexOf and substring etc to replace it
The problem here is how to replace only the string that doesn't have other things after it
To do this we can first look for indexOf the intended search string, add the length of the string, then check if the character after it doesn't match a certain set of characters, in which case we set the original string to the substring of the original up until the intended index, then plus the new string, then plus the substring of the original string starting from the length of the search string, to the end. And if the character after the search string DOES match the standard set of characters, do nothing
So let's try to make a function that does that
function replaceSpecial(original, search, other, charList) {
var index= original.indexOf(search)
if(index > -1) {
var charAfter = original [index + search.length]
if (!charList.includes(charAfter)) {
return original. substring (0, index) + other + original. substring (index+ search.length)
} else return original
} else return original
}
Then to use it with our example
var main ="Hi #jordan123 and #jordan!! have a good day"
var replaced = replaceSpecial (main, "#jordan", "#JordanAnderson", [0,1,2,3,4,5,6,7,8,9])

String fraction to numbers [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I have a string that represents a fraction and I want to extract the numerator and denominator.
var stringFraction = '99/99 (100%)';
var fraction = stringFraction.split(' ')[0].split('/');
console.log(0, Number(fraction[0]));
console.log(1, Number(fraction[1]));
This works fine but I'm wondering if a regex would be better?
Using a regex, you would be able to go for a simple match/find.
In your case, you first split the fraction itself from the remaining part, to then split again on the '/'.
In other words: a regex would allow you to reduce your code to a single match operation.
See here for some guidance how that would work.
Of course, you could also do that specific "matching" in a more manual mode:
get the string from 0 to index-1 of '/'
get the string from '/' to ' '
In other words, there are plenty of ways to retrieve that information. Each one has different pros and cons, and the real answer for a newbie learning this stuff: make experiments, and try them all.
There is no reason you can't do trimming and all with a single regex
without having to go through the gyrations with split.
Try this
/0*(\d+)\s*\/\s*0*(\d+)/
Formatted
0*
( \d+ ) # (1)
\s* / \s*
0*
( \d+ ) # (2)
JS sample
var strSample =
"0039/99 (100%)\n" +
"00/000 (100%)\n" +
"junk 102 / 487\n";
var NumerDenomRx = new RegExp( "0*(\\d+)\\s*/\\s*0*(\\d+)", "g");
var match;
while ( match=NumerDenomRx.exec( strSample ))
{
console.log("numerator = ", match[1] );
console.log("denominator = ", match[2] );
console.log( "-------------------------" );
}
If all strings have the same pattern
var stringFraction = '99/99 (100%)';
var fraction = stringFraction.match(/\d+/g); // result = ["99", "99", "100"];
Now technically this is shorter than spliting it, 15 vs 26 letters/signs/spaces, but only if the length of the array doesn't bother you. Otherwise you will have to chain extra method
.slice(1,-1)
that's +12 extra signs/letters. If the string is more complex
var fraction = stringFraction.match(/\d+\/\d+/)[0].split('/');
There are endless variations how to solve it really
P.S. Unless you got more complex strings, regex is not needed.

Regex Only one special character at a time from allowed special characters 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 4 years ago.
Improve this question
I want to allow 3 characters 1st Underscore (_) , 2nd hyphen (-) 3rd Dot (.)
but i want to put conditions that only one character is allowed at a time from all of them, and also only one time.
e.g
allowed usernames = abc.def , abc-def , abc_def
not allowed usernames = abc.de.f alert here(only one special character is allowed in a username)
not allowed usernames = abc.de-f , abc.de_f , ab-cd_ef
What should i do.
Try /^[a-z]*[-._]?[a-z]*$/
var tests = ['abc.def', 'abc-def', 'abc_def', 'abc.de.f','abc.de-f' , 'abc.de_f', 'ab-cd_ef'];
$.each(tests, function(a,b) {
$('body').append('<div>' + b + ' = ' + regIt(b) + '</div>');
});
function regIt(str) {
return /^[a-z]*[-._]?[a-z]*$/.test(str);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
^[a-z]*([-._]?)(?:[a-z]|\1)*$
This regex will match letters until it reaches the end of the string. If it reaches a symbol (- . or _) it will store that symbol as group 1, and keep matching for letters or that same symbol until the end of the string.
The following are matching examples:
an empty string
_something
something-something
foo_bar_baz
foo.
And here are some invalid strings:
my_file.txt
alpha.bravo_charlie
not-working_

javascript regex function [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 8 years ago.
Improve this question
I need to write JS regex (or function) that tells me if a string is in that format:
/root/:param1/:param2/:param3/.../
OR
/root/:param1/:param2/:param3/... (without last slash)
Any ideas?
Thanks.
If I'm interpreting your question correctly, it looks like we can break this pattern down into three primary components:
Start with /root
Followed by some number of /:param
Optionally followed by a /
Now we just need to develop the regular expressions for each component and combine them:
Start with /root
Start of the string is marked by ^ and we follow with /root
^/root
Followed by some number of /:param:
Let's say :param should match 1-N characters (+ operator) that are not a forward slash [^/]
This gives us /[^/]+
0-N of this entire unit can be matched using groups and the * operator: (/[^/]+)*
Optionally followed by a /
Use the ? operator: /?
Append a $ to specify the string's end
All together we get the regular expression ^/root(/[^/]+)*/?$. You can use RegExp.prototype.test to check for matches:
r = new RegExp('^/root(/[^/]+)*/?$')
r.test('/root') // => true
r.test('/root/') // => true
r.test('/root/apple/banana') // => true
r.test('/root/zebra/monkey/golf-cart/') // => true
If you're looking to match a URL path segment you'll need to use a more specific character set instead of the [^/] I used here for :param characters.

Categories