...

View Full Version : JS Search Array for substring then edit entry



Dxmxgxd
03-26-2012, 05:44 AM
Hi Guys,

I am trying to make a function that will search for a substring, in this case 'http://instagr.am/p/' within a text area on a form then add '' & '' tags around it.

I have currently got the following code



<script type='text/javascript'>
function show_alert() {
str = formname.elements['inputid'].value;
arr = (str.split(' ') + '<br />');
jQuery.each(arr, function() {
if (arr.indexOf('http://instagr.am/p/') >= 0) {
alert('An http://instagr.am/p/ link has been found');
//alert('It is entry ... in the array');
//Then edit the entry
}
}
);
}
</script>


This breaks the contents of the textarea (at spaces) into an array then finds any entries with 'http://instagr.am/p/' in them (could be 'http://instagr.am/p/29fdghHdv').

Once it has found any and all of these entries it will add an '' code to the beginning and an '' tag at the end.

Example...

http://instagr.am/p/vuHdeyfa2
is converted to:
'http://instagr.am/p/vuHdeyfa2'

Then the changes must reflected in the textarea input

"formname.elements["textareaid"].value = editedstring;


Thankyou for your assistance.

devnull69
03-26-2012, 07:39 AM
What is the problem?

Philip M
03-26-2012, 08:13 AM
Does this help?


<textarea id = "mytext" rows = "6" cols = "40" >Lorem veggies sunt bona vobis http://instagr.am/p/vuHdeyfa2 proinde vos postulo esse magis http://instagr.am/p/vuxx burdock groundnut salad pepper </textarea>

<script type = "text/javascript">

var txt = document.getElementById("mytext").value;
var arr = txt.split(" ");
for (var i = 0; i<arr.length; i++) {
if (arr[i].indexOf('http://instagr.am/p/') >= 0) { // found
var str = arr[i];
var newstr = "" + str + "";
changeVal2("mytext",str,newstr);
}
}

function changeVal2(IDS, passedV, change2){
var text = document.getElementById(IDS).value;
var match = new RegExp(passedV, 'gi'); // global and case-insensitive
var output = text.replace(match, change2); // replacing variable string with variable replacement value
document.getElementById(IDS).value = output;
}

</script>

We were lucky that a more serious incident wasn't averted. Presenter, Channel 4



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum