How to replace backslash in AS3 Flash? - regex

I'm trying to replace the backslashes of the url/path to get the file name of the swf:
var path:String = "C:\Test\myswf.swf"
var swfURL:String = path.replace(/\\/gi, "/");
var swfFileName:String = path.slice(path.lastIndexOf("/")+1, path.length).replace(".swf", "");
But it doesn't works. The response is: 'C:Testmyswf.swf'. It's doesn't replace the string path. How can i do it?

I think you might need to escape the backslashes within your URL string:
var path:String = "C:\\Test\\myswf.swf"

Try this regex:
\\([^\\.]+)\.
Then get $1

I've managed to solve this problem by "performing crazy" with the code below:
var reg:RegExp = new RegExp("\\d"+stage.loaderInfo.url, "gi");
var idswf:String = reg.toString().replace(/\\/g, "/");
idswf = idswf.split("/")[idswf.split("/").length-2].replace(".swf", "");
It may not be the best solution, but it worked well for me and can work for other people...

Related

regex to pick folders from network path

I'm trying to get a regex for selecting part of a network path
\\server.env.com\Target\Test1\Test2\final1\final2\final3\final4\final5
I need to skip two folders after Target and get the rest of the path from the above. So regex should give me final1\final2\final3\final4\final5 in this case. The path can have more levels of folders after final5. So the regex should work for any number of folders.
When I am using look behind, the browser says its not supported, so cannot use it.
Using regex...
var str1="path \\server.env.com\\Target\\Test1\\Test2\\final1\\final2\\final3\\final4\\final5"
// "path \server.env.com\Target\Test1\Test2\final1\final2\final3\final4\final5"
str1.match( /([^\\]*\\){5}(.*)/ )[2]
// "final1\final2\final3\final4\final5"
works based on a test for number of forward slashes prior to the 'finals'
Or, using split
var arr = str1.split("\\")
arr.splice(0,5)
var result = arr.join("\\")
result // "final1\final2\final3\final4\final5"
Following this post: How do you use a variable in a regular expression?
Create a regex that replaces everything up to Target and then two more sub-directories
var path = "\\server.env.com\\Target\\Test1\\Test2\\final1\\final2\\final3\\final4\\final5"
console.log("Path is: " + path)
var target = "Target"
var regex = "^.*" + target + "\\\\(?:[^\\\\]+\\\\){2}"
console.log("Regex is: "+ regex)
var re = new RegExp(regex, "mg")
var extracted = path.replace(re, "")
console.log("Extraction is: " + extracted)

Multiple regex to match file extension with version

My current regex is like so
/\.(jpe?g|png|gif|svg)$/i
I'm trying to modify it to support matching when the extension has get parameters at the end of it so all of the below formats would match
../fonts/fontawesome-webfont.svg
../fonts/fontawesome-webfont.svg?v=4.3.0
../fonts/fontawesome-webfont.svg?v=4.3.0#fontawesomeregular'
How can I modify it to support these?
Assuming the URLs to be parsed follow proper formatting (where only one '?' delimiter can be used to signify the start of the query) you could do:
/\.(jpe?g|png|gif|svg)(?:\?.*|)$/i
var urls = [
'../fonts/fontawesome-webfont.svg',
'../fonts/fontawesome-webfont.svg?v=4.3.0',
'../fonts/fontawesome-webfont.svg?v=4.3.0#fontawesomeregular'
];
var matches = urls.map(function(url) { return url.match(/\.(jpe?g|png|gif|svg)(?:\?.*|)$/i); });
document.write('<pre>' + JSON.stringify(matches, null, 2) + '</pre>');
Alternatively you could use Node's url.parse():
var url = require('url');
var urlObj = url.parse(URL_STRING);
var matches = urlObj.pathname.match(/\.(jpe?g|png|gif|svg)$/i);

Using Regex on Express with MongoDB

I want to use regex to find records that match certain pattern on Express.js using MongoDB.
Here is my code
var urlList = [];
var db = req.db;
var collection = db.get('project');
var regex = '/^'+url+'/';
console.log('regex = '+regex);
collection.find({url: { $regex: regex, $options: 'i' }},function(err,list){
console.log('length = '+list.length);
var arrayUrl = [];
for (var i=0;i<list.length;i++){
console.log(list[i].url);
arrayUrl.push(list[i].url);
}
});
But I got list.length = 0 although the database contains the records that match the pattern for sure.
Using the following command on cmd
db.project.find({url:{$regex:/^Projek-1/,$options: 'i'}});
I got the results I want.
How to use regex on express.js to find matched records in MongoDB database?
First, you don't really need to use $regex, you'll do fine with url: /foobar/i.
Anyway, the problem is that you are not creating a proper RegExp object, only a string that looks like one. Use a proper one by creating a new instance of RegExp
Example:
var re = new RegExp("^" + url);
...
find({url: re})

Regular expression in node js

I'm trying to strip out facebook.com from a URL. Can someone please help me with the RegEx to do this in NodeJs and express?
"http://www.facebook.com/RalphLauren"
I need to be left with "RalphLauren" as a string.
Thanks in advance!!!
Update:
This did the trick for what I wanted:
var url = 'http://www.facebook.com/RalphLauren';
var name = url.substr(url.lastIndexOf('/')+1);
No need for a regular expression. Use the parse method of the URL module and extract the path.
var parts = url.parse("http://www.facebook.com/RalphLauren");
console.log(parts.path); // '/RalphLauren'
Try
var newString = "http://www.facebook.com/RalphLauren".replace( /http:\/\/(www.)?facebook.com\/(.+)$/, "$2" );
You can use the replace method on strings
var url = "http://www.facebook.com/RalphLauren";
url.replace(/http:\/\/.*\.facebook\.com/, '');
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

Regular Expression help in AS3

I have to use tinymce as a wysiwyg editor in a CMS to populate a flash app. I need to strip out the modern HTML in favor of something flash can use. Here's what I'm trying:
var initUnderline:RegExp = new RegExp('<span style="text-decoration: underline;">', "gi");
var endUnderline:RegExp = new RegExp("</span>", "gi");
var string:String = $.xmlData.content.landing.overview;//load the content from xml
var safeStr:String = string.replace(initUnderline, '<span style="text-decoration: underline;"><u>');
safeStr = string.replace(endUnderline, '</u></span>');
however, this only works for the endUnderline RegExp. The initial is not being replaced. Ideas?
I'm not great with regExps at all!
There's nothing wrong with your regexp stuff per se.
The bug is that you need to run the second replace on safeStr, not on string:
var safeStr:String = string.replace(initUnderline, '<u>');
safeStr = safeStr.replace(endUnderline, '</u>');