Forum Moderators: open

Message Too Old, No Replies

How to remove characters from strings?

A function?

         

gerryw

3:42 pm on Jun 16, 2004 (gmt 0)



Hi all,
I am fairly new to javaScript and I am trying to write the code which removes a character/s from a string/s. As an example, if I initialise a variable 'myString' and use 'window.prompt' to input the string/s 'Weather' or 'Have a nice day', what would be the javaScript code to return the result (using 'document.write') 'Wthr' or 'ae a ie a'?

Regards
Gerry

DrDoc

3:56 pm on Jun 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld!

There is some great information on Regular Expressions here: [regular-expressions.info...]
Based on that, this is what a script like that would look like. You can toggle the

useOnlyVowels
variable...

<script type="text/javascript"> 
useOnlyVowels = true;
myText = "Weather";
noVowels = /[aeiou]/ig;
onlyVowels = /[^aeiou ]/ig;
if(useOnlyVowels) {
myText = myText.replace(onlyVowels,"");
}
else {
myText = myText.replace(noVowels,"");
}
document.write(myText);
</script>