AS replace all with or condition - actionscript-3

I would like to do a replace with two characters.
Below is my code, my problem is now i need to replace '/' as well rather than just '-', run replace twice is not really a good idea, and i am pretty bad at regular expression. Is anyone can help me write a RegExp which will search the whole string and replace any '-' o r'/' have.
var myPattern:RegExp = / /gi;
productId.replace(myPattern, '-')

Match any character within [] of your RegEx.
To replace both "/" and " " (space):
replace(/[\/ ]/g, "-");
Example:
var s:String = "2012/10/29 12:29";
trace(s.replace(/[\/ ]/g, "-"));
Would produce:
2012-10-29-12:29

this should replace any '/' or '-' in your productId string to a '$'
var myPattern:RegExp = /[\/-]/g;
productId.replace(myPattern, '$');

Related

Find the word and replace with html tag using regex

I have a text equation like: 10x^2-8y^2-7k^4=0.
How can I find the ^ and replace it with <sup>2</sup> in the whole string using regex. The result should be like:
I tried str = str.replace(/\^\s/g, "<sup>$1</sup> ") but I’m not getting the expected result.
Any ideas that can help to solve my problem?
I think you're looking for something like
\^(\d+)
It matches the ^, captures the exponent and replace with
<sup>$1</sup>
See it here at regex101.
Edit:
To meet your new demands, check this fiddle. It handles the sub as well using replace with a function.
Your current pattern matches a caret followed by a space character (space, tab, new-line, etc.), but you want to match a caret followed by a single character or multiple characters wrapped in accolades, as your string is in TeX.
/\^(?:([\w\d])|\{([\w\d]{2,})\})/g
Now, using str = str.replace(/\^(?:([\w\d])|\{([\w\d]{2,})\})/g, "<sup>$1</sup>"); should do the job.
You can make a more generic function from this expression that can wrap characters prefixed by a specific character with a specific tag.
function wrapPrefixed(string, prefix, tagName) {
return string.replace(new RegExp("\\" + prefix + "(?:([\\w\\d])|\\{([\\w\\d]{2,})\\})"), "<" + tagname + ">$1</" + tagname + ">");
}
For instance, calling wrapPrefixed("1_2 + 4_{3+2}", "_", "sub"); results in 1<sub>2</sub> + 4<sub>3+2</sub>.

Pulling URLs from objects for popup marker window

New to leaflet, and basically everything programming related.
I am making a brewery map showing locations of breweries, distilleries, vineyards, etc around the state.
What I want to do is have a popup that gives:
Name, Address, URL to that specific website.
I've figured out the Name/Address part, but I just can't figure out how to pull the URL from the object's properties. I've tried many iterations, none work (or even partially work).
As well, my searches have been fruitless, but I can't be the only one who has tried to do this. Bad search skills?
//load GeoJSON from an external file
$.getJSON("breweries.geojson",function(data){
var pintGlass = L.icon({
iconUrl: 'glass.png',
iconSize: [24,48]
});
var popupMarker = L.geoJson(data,{
pointToLayer: function(feature,latlng){
var marker = L.marker(latlng,{icon: pintGlass});
marker.bindPopup("<strong>" + feature.properties.NAME + "</strong> </br/>" + feature.properties.STREETNUM
+ " " + feature.properties.STREET + ", " + feature.properties.CITY + <a href=feature.properties.URL>feature.properties.URL</a>);
return marker;
}
});
var clusters = L.markerClusterGroup();
clusters.addLayer(popupMarker);
map.addLayer(clusters);
});
The last bit of the marker.bindPopup is the trouble spot. I've tried single quotes, double quotes, no luck. I tried creating a variable to pull the object.properties.URL out and insert that variable into the with no luck.
The problem is exactly at the following point, where you are trying to create a String:
+ <a href=feature.properties.URL>feature.properties.URL</a>
which should be
+ "" + feature.properties.URL + ""
It appears that you a not enclosing your strings correctly.
Try this and let me know if it works:
marker.bindPopup("<strong>" + feature.properties.NAME + "</strong></br/>" + feature.properties.STREETNUM + " " + feature.properties.STREET + ", " + feature.properties.CITY + " " + feature.properties.URL + "");
I know you've got a couple of "working" answers but i'de like to point out a few things. At the moment your ending up with markup like this:
<a href=http://example.org>http://example.org</a>
But it's best practice in HTML to make sure attribute values are wrapped in double quotes like this:
http://example.org
To accomplish that you'll have to do the following:
"" + feature.properties.URL + ""
Notice the slashes proceding the double quotes, a slash escapes the following double quote so that it gets treated like a string. Things like this can get pretty ugly very quick. That's why it's best when you're concatenating HTML with javascript that you simply use single quotes:
'' + feature.properties.URL + ''
That way you won't have to escape any double quotes in your strings.
And i'de like to point out a thing that Leaflet users often overlook is the wonderful L.Util.template method:
Simple templating facility, accepts a template string of the form 'Hello {a}, {b}' and a data object like {a: 'foo', b: 'bar'}, returns evaluated string ('Hello foo, bar'). You can also specify functions instead of strings for data values — they will be evaluated passing data as an argument.
http://leafletjs.com/reference.html#util-template
Using that takes away a lot of the hassle of what you're doing now, for example:
var values = {
a: feature.properties.NAME,
b: feature.properties.STREETNUM,
c: feature.properties.STREET,
d: feature.properties.CITY,
e: feature.properties.URL
};
var templateString = '<strong>{a}</strong><br>{b} {c}, {d} {e}';
var htmlString = L.Util.template(templateString, values);

Add a "/" between each word in a String

I've got this string :
var str:String = mySharedObject.data.theDate;
where mySharedObject.data.theDate has some words (not always the same words has it depends on which button the user clicked).
So mySharedObject.data.theDate = "words words words".
Is it possible to add a "/" between each word ? (without knowing which words are in mySharedObject.data.theDate).
In order to have:
mySharedObject.data.theDate = "words/words/words".
Edit : You can replace " " with "/" in your string, this will split string with " " separator and then join with "/"
mySharedObject.data.theDate= mySharedObject.data.theDate.split(" ").join("/")
You can also do that using String.replace() with a little regular expression which will replace all spaces (notice here the g (global) flag to replace all instances), like this :
var s:String = 'word word word';
trace(s.replace(/\s/g, '/')); // gives : word/word/word
And for more about regular expressions take a look here.
Hope that can help.

Regexp for mysql to detect one '=' and not two '=='

I want to detect the following:
window.location.href = "http://newlocation.com";
but not
window.location.href == "http://newlocation.com";
the query is ran in mySQL like so:
select "blablabla" REGEXP "bla"
Can't seem to get my head around this one.
SELECT 'window.location.href="http://newlocation.com"' REGEXP "href=\""
This gives me 1 (TRUE)
That one also
SELECT 'window.location.href="http://newlocation.com"' REGEXP "window\.location\.href=\"http:\/\/newlocation\.com\""
It depends what you want to match really.
There are two ways:
SELECT 'window.location.href = "http://www.google.com/"' REGEXP '[[:<:]]=[[:>:]]';
OR
SELECT 'window.location.href = "http://www.google.com/"' REGEXP ' = ';
Please note that both the above assume that there will be a space before and after the equals sign. If there might be no spaces before or after equals, you can try:
SELECT 'window.location.href = "http://www.google.com/"' REGEXP '[^=]+=[^=]+';
I'm not sure about the last one but it should work.
Hope it helps!

delete leading characters

I'm a noob to actionscript so this should be easy:
How do I delete leading characters from a string? I have a string that contains (at times) both numeric & non-numeric characters. If I want to delete all the leading 9's, how would I do that?
var testVar:String = '999998gjek74k';
I want the testVar to be 'gjek74k'.
So far, I have (though not working):
var testVar:String = '999998gjek74k';
testVar.replace(/^0/g, "");
.replace doesn't modify the string. It returns the replaced string.
testVar = testVar.replace(/^\d+/, '');
(Also the pattern /^0/g is wrong, as commented by #santa).
Assuming you are testing the variables and not multiple lines:
private var testVar = testVar.replace(/^\d*(.+)$/,"$1");