Add a "/" between each word in a String - actionscript-3

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.

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);

Combining two regular expressions in java

I have some text like this:
[image]bdaypic.jpg[caption]My pic[/caption][/image]
I should get output as:
<figure><img src='bdaypic.jpg'/><figcaption>My pic</figcaption></figure>
I'm using the below code:
string = string.replaceAll("\\[image\\](.*?)\\[\\/image\\]", "<figure><img src='$1'/></figure>");
string = string.replaceAll("\\[caption\\](.*?)\\[\\/caption\\]", "<figcaption>$1</figcaption>");
but i'm getting output as
<figure><img src='bdaypic/><figcaption>My pic</figcaption>'</figure>
[caption][/caption] is optional.If it there then only it should be replaced with tag.
The problem seems to lie within the first replace. You are capturing everything between the [image] tag and replacing it with a quoted version of the content. I merged your two expressions and got what you are after:
String string = "[image]bdaypic.jpg[caption]My pic[/caption][/image]";
string=string.replaceAll("\\[image\\](.*?)\\[caption\\](.*?)\\[\\/caption\\]\\[\\/image\\]","<figure><img src='$1'/><figcaption>$2</figcaption></figure>");
System.out.println(string);
Yields:
<figure><img src='bdaypic.jpg'/><figcaption>My pic</figcaption></figure>
Lot of crazy escaping but you can use:
str = str.replaceAll("\\[(image)\\]([^\\[]+)\\[(caption)\\]([^\\[]+)\\[/\\3\\]\\[/\\1\\]",
"<figure><img src='$2'/><figcaption>$4</figcaption></figure>");
RegEx Demo
Ive created a function that will do just that:
function showreplaced(){
var str="[image]bdaypic.jpg[caption]My pic[/caption][/image]";
var delimitedStr=str.replace("[image]","").replace("[/caption][/image]","").replace("[caption]","|");
var src=delimitedStr.substring(0,delimitedStr.indexOf("|"));
var caption=delimitedStr.substring(delimitedStr.indexOf("|")+1);
var finalStr="<figure><img src='" + src + "'/><figcaption> " + caption + "</figcaption></figure>";
alert(finalStr);
}

AS replace all with or condition

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, '$');

Remove all but the first word from a sentence

I need to find a way to take a sentence and remove all its words besides the first.
If the sentence is "Hi my name is dingo"
I need to get only the word "Hi"
var sentence : String = "Hi my name is dingo"
var words : Array = sentence.split( " " );
var firstWord : String = words[ 0 ];
trace( firstWord ) // outputs "Hi"
Obviously this only works for simple sentences without punctuation. If you need more complex word parsing you can use regexp:
var pattern : RegExp = new RegExp( "\\b.(\\w*).\\b",'gi' );
var words : Array = sentence.match( pattern );