flex match regex - regex

I have asked this question before and got some reponses but i am still not cleared on how to do this, if someone could help me more i would appreciate.
What i am doing is reading some information from a URL then i am using a regex(match) to get the string that i need. However i need the result for each match to be a string because i need to manipulate this string. below is the code tha t i have. Any help is appeciated.Again i have asked this but i am not sure how to proceed on my side.
var composer:StandardFlowComposer = txtSource.textFlow.flowComposer as StandardFlowComposer;
var pattern:RegExp= new RegExp("href=\"/player/.*?\">");
var pattern2:RegExp= new RegExp("href=\"/player/.*?\"");
var myArrayOfLines:Array = ul.data.split(/\n/);
var line:Array;
var str:String = "";
var lineFinal:Array;
var lineURL:String;
lineFinal = myArrayOfLines;
for each (var lineRaw:String in myArrayOfLines)
{
trace(lineRaw);
}
trace(line);
Anymore help is appreciated. thank you very much.

Try this , this will matches the specifed pattern against the string and returns array of strings...
var myArrayOfLines:Array = ul.data.match(/"your pattern here"/gs);
for each (var lineRaw:String in myArrayOfLines)
{
trace(lineRaw);
}
trace(line);
Try your regex here

Related

Typescript regex exclude whole string if followed by specific string

I'm been running into weird issues with regex and Typescript in which I'm trying to have my expression replace the value of test minus the first instance if followed by test. In other words, replace the first two lines that have test but for the third line below, replace only the second value of test.
[test]
[test].[db]
[test].[test]
Where it should look like:
[newvalue]
[newvalue].[db]
[test].[newvalue]
I've come up with lots of variations but this is the one that I thought was simple enough to solve it and regex101 can confirm this works:
\[(\w+)\](?!\.\[test\])
But when using Typescript (custom task in VSTS build), it actually replaces the values like this:
[newvalue]
[newvalue].[db]
[newvalue].[test]
Update: It looks like a regex like (test)(?!.test) breaks when changing the use cases removing the square brackets, which makes me think this might be somewhere in the code. Could the problem be with the index that the value is replaced at?
Some of the code in Typescript that is calling this:
var filePattern = tl.getInput("filePattern", true);
var tokenRegex = tl.getInput("tokenRegex", true);
for (var i = 0; i < files.length; i++) {
var file = files[i];
console.info(`Starting regex replacement in [${file}]`);
var contents = fs.readFileSync(file).toString();
var reg = new RegExp(tokenRegex, "g");
// loop through each match
var match: RegExpExecArray;
// keep a separate var for the contents so that the regex index doesn't get messed up
// by replacing items underneath it
var newContents = contents;
while((match = reg.exec(contents)) !== null) {
var vName = match[1];
// find the variable value in the environment
var vValue = tl.getVariable(vName);
if (typeof vValue === 'undefined') {
tl.warning(`Token [${vName}] does not have an environment value`);
} else {
newContents = newContents.replace(match[0], vValue);
console.info(`Replaced token [${vName }]`);
}
}
}
Full code is for the task I'm using this with: https://github.com/colindembovsky/cols-agent-tasks/blob/master/Tasks/ReplaceTokens/replaceTokens.ts
For me this regex is working like you are expecting:
\[(test)\](?!\.\[test\])
with a Typescript code like that
myString.replace(/\[(test)\](?!\.\[test\])/g, "[newvalue]");
Instead, the regex you are using should replace also the [db] part.
I've tried with this code:
class Greeter {
myString1: string;
myString2: string;
myString3: string;
greeting: string;
constructor(str1: string, str2: string, str3: string) {
this.myString1 = str1.replace(/\[(test)\](?!\.\[test\])/g, "[newvalue]");
this.myString2 = str2.replace(/\[(test)\](?!\.\[test\])/g, "[newvalue]");
this.myString3 = str3.replace(/\[(test)\](?!\.\[test\])/g, "[newvalue]");
this.greeting = this.myString1 + "\n" + this.myString2 + "\n" + this.myString3;
}
greet() {
return "Hello, these are your replacements:\n" + this.greeting;
}
}
let greeter = new Greeter("[test]", "[test].[db]", "[test].[test]");
let button = document.createElement('button');
button.textContent = "Say Hello";
button.onclick = function() {
alert(greeter.greet());
}
document.body.appendChild(button);
Online playground here.

Google Apps Script - find string and return the following characters

The output in the log should be " scripting" because these are the next 10 characters followed by the search criteria "general-purpose". Please visit www.php.net to see what I mean, you will find the search string "general-purpose" on top of www.php.net. I think that I have done some more mistakes in this piece of code, right?
function parse() {
// parse site and store html in response
var response = UrlFetchApp.fetch('www.php.net').getContentText();
// declare search string and new regex object
var str = "/general-purpose/+10-following-charcters";
var regExp = new RegExp("/general-purpose/.{0,10}", "gi");
// find the string "general-purpose" and store the next 10 characters in response
var response = regExp.exec(str[0]);
// expected result in logger output is " scripting"
Logger.log(response);
}
It should be general-purpose(.{0,10}) and not /general-purpose/.{0,10}.
Also regExp.exec(str[0]) should be regExp.exec(str)[1].
This code seems to work fine
var str = UrlFetchApp.fetch('www.php.net').getContentText();
var regExp = new RegExp("general-purpose(.{0,10})", "gi");
var response = regExp.exec(str)[1];
Logger.log(response);

What is the equalient of JavaScript's "s.replace(/[^\w]+/g, '-')" in Dart language?

I am trying to get the following working code in JavaScript also working in Dart.
https://jsfiddle.net/8xyxy8jp/1/
var s = "We live, on the # planet earth";
var results = s.replace(/[^\w]+/g, '-');
document.getElementById("output").innerHTML = results;
Which gives the output
We-live-on-the-planet-earth
I have tried this Dart code
void main() {
print( "We live, on the # planet earth".replaceAll("[^\w]+","-"));
}
But the output becomes the same.
What am I missing here?
If you want replaceAll() to process the argument as regular expression you need to pass a RegExp instance. I usually use r as prefix for the regex string to make it a raw string where not interpolation ($, \, ...) takes place.
main() {
var s = "We live, on the # planet earth";
var result = s.replaceAll(new RegExp(r'[^\w]+'), '-');
print(result);
}
Try it in DartPad

Replace parts of a URL in Greasemonkey

I'm trying to replace a part of url using a Greasemonkey script, but having hard time to achieve what I'm trying to do.
Original Urls are like:
http://x1.example.to/images/thumb/50/157/1571552600.jpg
http://x2.example.to/images/thumb/50/120/1201859695.jpg
http://x3.example.to/images/thumb/50/210/2109983330.jpg
What I want to achieve is this:
http://example.to/images/full/50/157/1571552600.jpg
http://example.to/images/full/50/120/1201859695.jpg
http://example.to/images/full/50/210/2109983330.jpg
I just want to replace thumb with full and cut out the x1.example.to, x2.example.to, x3.example.to, x4.example.to etc.. part completely from the original URL so new urls will be starting like example.to/images/full/
How do I achieve this?
I have found a Greasemonkey script from this answer and did try to work out but failed.
Here's what i did so far.
// ==UserScript==
// #name Example Images Fixer
// #namespace Example
// #description Fixes image galleries
// #include http://*.example.to/*
// ==/UserScript==
var links = document.getElementsByTagName("a"); //array
var regex = /^(http:\/\/)([^\.]+)(\.example\.to\/images\/thumb/\)(.+)$/i;
for (var i=0,imax=links.length; i<imax; i++) {
links[i].href = links[i].href.replace(regex,"$4full/$5");
}
Any help on that?
You're forgetting to put the http:// part in your replacement URL:
/^(https?:\/\/)[^.]+\.(example\.to\/images\/)thumb\/(.+)$/i
and then:
.replace(regex, "$1$2full/$3");
You can see the results here.
Here is an example of what can be done:
var urls = ['http://x1.example.to/images/thumb/50/157/1571552600.jpg',
'http://x2.example.to/images/thumb/50/120/1201859695.jpg',
'http://x3.example.to/images/thumb/50/210/2109983330.jpg'];
for (var i = 0, len = urls.length; i < len; i++) {
urls[i] =
urls[i].replace(/:\/\/[^\.]+\.(example.to\/images\/)thumb/, '://$1full');
console.log(urls[i]);
}
/* result
"http://example.to/images/full/50/157/1571552600.jpg"
"http://example.to/images/full/50/120/1201859695.jpg"
"http://example.to/images/full/50/210/2109983330.jpg"
*/
Here is the Fiddle

AS3 RegEx returns null

Can anyone explain why the code below traces null when on the timeline?
var cleanRegExp:RegExp = /^[a-zA-Z0-9]+(\b|\/)/;
var str:String = "/num83r5/and/letters/4/A/";
trace(str.match(cleanRegExp.toString()));
I've read the documentation, so I'm pretty sure that I'm declaring the RegEx correctly and that String.match() should only return null when no pattern is passed in, otherwise it should be an array with 0+ elements. I suspected a badly written expression, but surely that should still return an empty array?
EDIT: Both these trace "no matches" instead of either 5 or 0, depending on the expression being correct:
var cleanRegExp:RegExp = /^[a-zA-Z0-9]+(\b|\/)/;
var str:String = "/num83r5/and/letters/4/A/";
var res:Array = str.match(cleanRegExp);
trace((res == null) ? "no matches" : res.length);
And:
var cleanRegExp:RegExp = /^[a-zA-Z0-9]+(\b|\/)/;
var str:String = "/num83r5/and/letters/4/A/";
var res:Object = cleanRegExp.exec(str);
trace((res == null) ? "no matches" : res[0]);
UPDATE
If you're going to work in flash with regex, this tool is a must-have:
http://gskinner.com/RegExr/
http://gskinner.com/RegExr/desktop/
ORIGINAL ANSWER
Don't use toString(), you're then doing a literal search, which will include the addition of all of your regex formatting, including flags. Do:
str.match(cleanRegExp);
In fact the proper method is to reference the returned object like so:
var results:Array = str.match(cleanRegExp);
if(results != null){
//We have a match!
}