Actually I want to add "*." at opening and ";" at ending on each array element. Below is the sample code.
var ext = getExt(filename);
var s = '<%=AllowedExtensions %>';
var match = s.split(', ');
In the above code in 'match' I use to get ["jpg", "png", "txt"] like this. So here on each array element I want to prefix with "*." and at end of each want to add ";" and also it should give me a string such as "*.jpg;*.png;*.txt;" .
So please help me out on this to achieve this target.
So currently match holds an array of extensions correct? Loop through each of the array items and take each item and add *. and ; to each one then do what you need after that with them. Could be as simple as this:
for (var str in match) {
str = '*.' + str + ';';
}
Remember to mark as answer if this post answered or solved your problem.
You can make replacement in given string before to split it in array, using regular expression like in example, given below
<HTML>
<HEAD>
<script>
function a()
{
var str=" , .jpg , .gif , .png"; //example of string, which includes these extensions
str=str.replace(/.jpg/g, "*.jpg;");
str=str.replace(/.gif/g, "*.gif;");
str=str.replace(/.png/g, "*.png;");
alert(str); //show result fot test
}
</script>
</HEAD>
<BODY OnLoad="a()">
Result in alert
</BODY>
</HTML>
tripati_tutu
Member
30 Points
53 Posts
How to add a string into each and every string in an array by using javascript or jquery?
Feb 15, 2012 02:15 PM|LINK
Actually I want to add "*." at opening and ";" at ending on each array element. Below is the sample code.
var ext = getExt(filename); var s = '<%=AllowedExtensions %>'; var match = s.split(', ');In the above code in 'match' I use to get ["jpg", "png", "txt"] like this. So here on each array element I want to prefix with "*." and at end of each want to add ";" and also it should give me a string such as "*.jpg;*.png;*.txt;" .
So please help me out on this to achieve this target.
Thanks.
javascript
tripati_tutu
Member
30 Points
53 Posts
Re: How to add a string into each and every string in an array by using javascript or jquery?
Feb 15, 2012 02:21 PM|LINK
b471code3
Star
13877 Points
2598 Posts
Re: How to add a string into each and every string in an array by using javascript or jquery?
Feb 15, 2012 02:24 PM|LINK
So currently match holds an array of extensions correct? Loop through each of the array items and take each item and add *. and ; to each one then do what you need after that with them. Could be as simple as this:
for (var str in match) { str = '*.' + str + ';'; }Zhelezov
Member
278 Points
65 Posts
Re: How to add a string into each and every string in an array by using javascript or jquery?
Feb 15, 2012 03:03 PM|LINK
You can make replacement in given string before to split it in array, using regular expression like in example, given below
<HTML>
<HEAD>
<script>
function a()
{
var str=" , .jpg , .gif , .png"; //example of string, which includes these extensions
str=str.replace(/.jpg/g, "*.jpg;");
str=str.replace(/.gif/g, "*.gif;");
str=str.replace(/.png/g, "*.png;");
alert(str); //show result fot test
}
</script>
</HEAD>
<BODY OnLoad="a()">
Result in alert
</BODY>
</HTML>
javascript