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

03-26-2012, 06: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

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.


is converted to:

Then the changes must reflected in the textarea input

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

Thankyou for your assistance.

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

Philip M
03-26-2012, 09: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 + "";

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;


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