Using RegExp for separate each five-character by '-' or something - regex

Hello i have a TextField on my scene. It haves only digits, user input them by clicking on buttons (NumPad). I want to separate each five-character by '-' . User must input 20 digits
I'm using this code
var rexp:RegExp = new RegExp("/^((\d{0,5})(\d{0,5})(\d{0,5})(\d{0,5}))$/");
trace(rexp.test(textEnter.text)); //always false
var s:String=textEnter.text.replace(rexp, "$2-$3-$4-$5");
trace(s);//format is fails
textEnter.text = s;
On this site my patterns are working fine. But they aren't working in my AS3 script.
can you help me and say what i'm doing wrong.

There are two ways to instantiate a regex in AS3. You can either use the constructor form or the regex literal form.
Literal form
With this notation, the regex literal creates an implicit regex object, so you don't have to. The following works fine:
var rexp:RegExp = /^((\d{0,5})(\d{0,5})(\d{0,5})(\d{0,5}))$/;
Constructor form
If you really want to use the constructor form to instantiate your regex, then you replace the delimiting forward slashes with quotation marks. You must also add a second backslash to any metasequences.
var rexp:RegExp = new RegExp("^((\\d{0,5})(\\d{0,5})(\\d{0,5})(\\d{0,5}))$");
More info: http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118a9b90204-7e92.html

Related

how to implement regex in Angular ng-select?

I have already implemented angular multi-select Dropdown. Now I want it to search using RegEx. Like if I have qwertyuiop, and if I wrote w*i than it should suggest me all the entries who contains 'W' and 'I' in the same string.
Do you want to know the whole thing, like how to bind the input of an input field, read the input, create an RegEx out of it, and then use the RegEx as a filter to the list you are showing in your dropdown ?
Or only the RegEx part?
For the part with RegEx:
You should take a look at the Javascript Defintion of RegEx (for example at Mozilla Developer Network ). It has a quite nice functionality.
let input:string = // the value the user typed, like w*i
const regEx = new RegExp(input)
let myDropdownList:string[] = // the list of strings i want to filter
let filteredDropdownList = myDropdownList.forEach((value:string)=>{
return regEx.test(value)
})
What happens here?
You are creating a regular Expressen with new RegExp(someString).
You can optimize it with RegEx flags.
Later you test a string with myRegEx.test(theString). It will return true if the regEx founds at least one match in the string.
I hope this helps you a bit.
warm regards.

Regular Expression for String without a "?" character to redirect to string with "?" character

On our website we occasionally experience an error where dynamic links aren't building correctly.
URLs like this
https://www.test.url.edu/collections/&edan_fq[]=p.edanmdm.indexedstructured.object_type:%22Financial+records%22&edan_fq[]=p.edanmdm.descriptivenonrepeating.record_id:item_*
Should actually be this:
https://www.test.url.edu/collections/search?edan_fq[]=p.edanmdm.indexedstructured.object_type:%22Financial+records%22&edan_fq[]=p.edanmdm.descriptivenonrepeating.record_id:item_*
We want to create a regular expression to redirect
/collections/&edan_fq[]=
to
/collections/search?edan_fq[]=
But everything after "edan_fq[]=" can change dynamically--there are thousands of permutations of the string after that point.
Does anyone know how this would be done?
If you use \& without Global Flag in Regex it will give first match. I've used JavaScript, please check this.
var data = "https://www.test.url.edu/collections/&edan_fq[]=p.edanmdm.indexedstructured.object_type:%22Financial+records%22&edan_fq[]=p.edanmdm.descriptivenonrepeating.record_id:item_*";
var regex = /\&/
data = data.replace(regex,"search?");
console.log(data);
Please check Substitution example in Regex101.

Javascript Storing Regexp screws the original format

I have an angular app where a user can add a regexp in a form, a value like:
github\.com/([A-Za-z0-9\-\_]+)/([A-Za-z0-9\-\_]+)
When I store this in the localStorage and I inspect the localStorage:
github\\\\.com\\/([A-Za-z0-9\\\\-\\\\_]+)\\/([A-Za-z0-9\\\\-\\\\_]+)
When I retrieve in Javascript elsewhere this value I get:
github\\.com\/([A-Za-z0-9\\-\\_]+)\/([A-Za-z0-9\\-\\_]+)
This is not the original regexp and the match method in Javascript can't work.
NOTE: after submitting the form, I store the object with:
localStorage.myobject = JSON.stringify(myobject);
You can get rid of overescaping here, just use
github[.]com/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)
and initialize it via a RegExp constructor so as not to have to escape the regex delimiter /. A dot inside [] loses its special meaning and only matches a literal dot, the hyphen at the end of the character class only matches a literal hyphen, and the _ does not have to be escaped at all anywhere in the pattern:
var tst = "github.com/Test1-Text/Test2";
var pattern = "github[.]com/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)";
console.log(new RegExp(pattern).test(tst));
UPDATE:
When using patterns from external sources, you need to use the constructor notation. Make sure your regex patterns are stored as literal strings (if you had RegExp("C:\\\\Folder"), make sure it is stored as C:\\Folder), and when reading the value in it will be automatically be usable with the RegExp constructor.

RegEx to cut out URL

I try to get an URL from a String of the following format:
RANDOMRUBBISHhttps://www.my-url.com/randomfirstname_randomlastnameRANDOMRUBBISH
I already tried some things, especially the the look before/after, which I used before successfully on another url format (starts https... ends .html, this was working).
But seems I'm too stupid to figure out the regex for the kind of string mentioned above. I just want the URL part from https.... to the end of the random last name. Is this even possible?
Any Ideas?
If you can guarantee that randomfirstname_randomlastname is all lowercase and RANDOMRUBBISH is all uppercase, you can use character classes [a-z] and [A-Z]. The language the regex is for will determine how to use these.
This is example works in javascript:
var str = "RANDOMRUBBISHhttps://www.my-url.com/randomfirstname_randomlastnameRANDOMRUBBISH";
var match = /https:\/\/www\.my-url\.com\/[a-z]*/.exec(str);

RegEx check if string contains certain value

I need some help with writing a regex validation to check for a specific value
here is what I have but it don't work
Regex exists = new Regex(#"MyWebPage.aspx");
Match m = exists.Match(pageUrl);
if(m)
{
//perform some action
}
So I basically want to know when variable pageUrl will contains value MyWebPage.aspx
also if possible to combine this check to cover several cases for instance MyWebPage.aspx, MyWebPage2.aspx, MyWebPage3.aspx
Thanks!
try this
"MyWebPage\d*\.aspx$"
This will allow for any pages called MyWebPage#.aspx where # is 1 or more numbers.
if (Regex.Match(url, "MyWebPage[^/]*?\\.aspx")) ....
This will match any form of MyWebPageXXX.aspx (where XXX is zero or more characters). It will not match MyWebPage/test.aspx however
That RegEx should work in the case that MyWebPage.aspx is in your pageUrl, albeit by accident. You really need to replace the dot (.) with \. to escape it.
Regex exists = new Regex(#"MyWebPage\.aspx");
If you want to optionally match a single number after the MyWebPage bit, then look for the (optional) presence of \d:
Regex exists = new Regex(#"MyWebPage\d?\.aspx");
I won't post a regex, as others have good ones going, but one thing that may be an issue is character case. Regexs are, by default, case-sensitive. The Regex class does have a static overload of the Match function (as well as of Matches and IsMatch) which takes a RegexOptions parameter allowing you to specify if you want to ignore case.
For example, I don't know how you are getting your pageUrl variable but depending on how the user typed the URL in their browser, you may get different casings, which could cause your Regex to not find a match.