Page 1 of 1
Dynamically replace double dashes in JavaScript?
Posted: Mon Mar 30, 2009 12:02 pm
by JAB Creations
I've been working on a simple JavaScript function that dynamically puts together a
preview URL.
The only problem is if you try to make a title such as 'Example - Test' it ends up becoming 'example___test' where as I want to replace two or more instances of dashes with a single dash.
Here is my function...
Code: Select all
function cms_url(){ var file0 = document.getElementById('meta_title').value.toLowerCase(); var file1 = file0.replace(/ /g, '-'); var file2 = file1.replace(/_/g, '-'); var file3 = file2.replace(/--/g, '-'); var file4 = file3.replace(/--/g, '-'); alert(file4); document.getElementById('cms_url').firstChild.nodeValue=document.getElementById('meta_section').options[document.getElementById('meta_section').selectedIndex].getAttribute('title')+'/'+file2;}
Re: Dynamically replace double dashes in JavaScript?
Posted: Mon Mar 30, 2009 3:22 pm
by php_east
Code: Select all
<?php
if (!empty($_POST['regex_line'])) print_r(@$_POST['regex_line']); else $_POST['regex_line']='__XXX______XX___Y_X_Z__123';
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input name="regex_line" id="regex_line" type="text" value="<?php echo @$_POST['regex_line']; ?>" />
<input type="submit" />
</form>
<script type="text/javascript">
function remove_double__(str)
{
var regex_str = "(__)+";
var re = new RegExp(regex_str,'gm');
c=0;
if (str.match(re))
{
while (str.match(re))
{
c++; if (c>1000) break;
str = str.replace(RegExp.lastParen,'_');
}
return (str);
}
return (str);
}
element = document.getElementById('regex_line');
text = element.value;
element.value = remove_double__(text);
</script>
<?php
what you will need is probably just the javascript part.
Note : the c counter is safeyt net, breaking out if more than 1000 loops occur. you can remove it if not needed, no harm leaving it ( unless you are expecting a thousand __) .
Re: Dynamically replace double dashes in JavaScript?
Posted: Mon Mar 30, 2009 5:42 pm
by JAB Creations
It took me a little while but I got it working, thanks!
Code: Select all
<?php if (!empty($_POST['regex_line'])) print_r(@$_POST['regex_line']); else $_POST['regex_line']='__XXX__-----____XX___Y_X_Z__123';?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input id="regex_line" name="regex_line" type="text" value="<?php echo @$_POST['regex_line'];?>" />
<input onclick="remove_double__();" type="button" value="clicky clicky" />
<input type="submit" />
</form>
<script type="text/javascript">
function remove_double__()
{
var str = document.getElementById('regex_line').value;
var regex_str = '(--)+';
var re = new RegExp(regex_str,'gm');
var c=0;
if (str.match(re))
{
while (str.match(re))
{
alert(str);
c++; if (c>1000) break;
str = str.replace(RegExp.lastParen,'-');
}
alert(str);
return (str);
}
return (str);
}
var element = document.getElementById('regex_line');
var text = element.value;
element.value = remove_double__(text);
</script>
Re: Dynamically replace double dashes in JavaScript?
Posted: Mon Mar 30, 2009 8:16 pm
by JAB Creations
I've merged all the code and cleaned it up a little better for others...
Code: Select all
<?php if (!empty($_POST['regex_line'])) print_r(@$_POST['regex_line']); else $_POST['regex_line']='__XXX__-----____XX___Y_X_Z__123';?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input id="regex_line" name="regex_line" type="text" value="<?php echo @$_POST['regex_line'];?>" />
<input onclick="remove_double__();" type="button" value="clicky clicky" />
<input type="submit" />
</form>
<script type="text/javascript">
function remove_double__()
{
var str0 = document.getElementById('regex_line').value;
var str1 = str0.replace(/ /g, '-');
var str = str1.replace(/_/g, '-');
var regex_str = '(--)+';
var re = new RegExp(regex_str,'gm');
var c=0;
if (str.match(re))
{
while (str.match(re))
{
//alert(str);
c++;
if (c>1000) {break;}
str = str.replace(RegExp.lastParen,'-');
}
alert(str);
return (str);
}
return (str);
}
var element = document.getElementById('regex_line');
var text = element.value;
element.value = remove_double__(text);
</script>