JavaScript - String.replace Question
Hi,
I'm fairly new to javascript as well as brand new to these forums. I have a question that I'm sure has a simple answer but I either can't get my head around it or I'm just too unfamiliar with string methods. I'm trying to replace all instances of "&" with its alt code "&" in a string. Below is my code.. Code: Match = subText.indexOf("&"); while (Match != -1) { subText = subText.replace( "&", "&" ) Match = subText.indexOf("&"); } The problem here is that an ampersand will always exist in the string if there was one originally and I get an infinite loop. Would anyone know of a way around this? Thank you. Similar Tutorialsi am trying to make an online graphing calculator with javascript. dont ask how because i dont know. but there is an annoying error in a do...while loop. although it should break out of the loop when the |'s (absolute value signs) are replaced with Math.abs( and ). here is the code. Code: var initec = function(){ var rg = { } ; rg.matc = false; rg.i = 0; rg.change = function(equ){ if (typeof(equ) != "string"){ alert('Equation must be a string'); return; } alert("starting equation: "+ equ); rg.i = 0; do{ rg.matc = equ.match(/\|/); if(rg.i === 0){ equ.replace(/\|/, " Math.abs("); rg.i = 1; alert("1 "+equ); } else { equ.replace(/\|/, " ) "); rg.i = 0; alert("0 "+equ); } }while(rg.matc) alert("finished equation: " + equ); } return rg; } rg=initec(); rg.change("|8/x+7|-2"); the last 2 lines and the alerts are for debugging. as you can see, it is not finished. but still, it should work. hello, I'm trying to build a simple function to append and replace an argument in a query string, for example, if my url was like; www.domain.com/page.php?test=123&blah=abc and I want to add on qty=5 like; www.domain.com/page.php?test=123&blah=abc&qty=5 ok, no problem there, however if I now want to update that to qty=6, i get this: http://www.domain.com/page.php?test=...bc&qty=5&qty=6 my code thus far; Code: function AppendURL(){ var AURL = document.getElementById('AddToCartLink'); var numQty = document.getElementById('Qty').value; var TempURL = AURL.href; AURL.href = TempURL + '&Qty=' + numQty; } Can some one help me, I want to replace the characters from a string and I have tried with this script: Code: var v5=form.description.value.replace(/#/, "%23"); But it only changes the first character from the string. Turning #### into %23### but I want them all transformed. I would like to replace all the characters within a string with asterisk using javascript. Ex: If i have a string "test123". Replaced string should be "*******". Could you please let me know how i can implement this using Regex within javascript. Hi, I'm just wandering, how can you stop the replace() function from replacing characters inside a word, such as, if I try to replace 'x' with 'and', then my string would go from 'example, x' to 'eandample, and'. Thanks in advanced Hi, I am trying to write some code that will test if a user enters a non-numeric character in an input text field, and if so, replaces it with a '0' Code: <script type="text/javascript"> function IsNumVal (){ for (J=1; J<=3; J++){ if (isNaN(Number(document.getElementById('Intext' + J).value))){ document.getElementById('Intext' + J).value = 0; } } } </script> <Body> <table border="1" cellspacing="1" id = "Numbers"> <tr id="RowNum1"> <td ><input type="text" size = "5" name="Intext1" id="Intext1" value = "3" onkeyup="IsNumVal()" /></td> </tr> <tr id="RowNum2"> <td><input type="text" size = "5" name="Intext2" id="Intext2" value = "9" onkeyup="IsNumVal()" /></td> </tr> <tr id="RowNum3"> <td><input type="text" size = "5" name="Intext3" id="Intext3" value = "18" onkeyup="IsNumVal()" /></td> </tr> </table> </Body> document.getElementById('Intext' + J).value = 0; doesn't work document.Schedule.'Intext' + J.value = 0; doesn't work document.Schedule.Intext2.value = 0; works, but I need to test each text field while looping, so I can't "hard code" the ID. Suggestions? How do I replace the string "rpg" only present in between <et> tags ? Code: <et>ReplaceOnlyrpg</et> After replacement it should look like this Code: <et>ReplaceOnly</et> Any advice is appreciated. Thank you I know how the replace method works, but I can't for the life of me figure out exactly what this replace function is doing... Can someone shed somelight for me? the g I *think* is a flag for global replacement... Code: replace(/\s/g, " "); I know that *something* is being replaced by a non-blank space, but what? Hi, I'm trying to get rid of HTML syntax in a string - that is, anything between < > brackets, including the < > brackets, I want to remove. So I tried this, but I'm probably not doing it right... any tips? var myNewString = info.replace(/<[.]>/g, ""); Hello, I need your help. Let's say for example if I have the following file number: 2011-1234567 and if the value of the variable is already the same (ie. var fileNo = 2011-1234567) how can I go about automatically adding a number at the end? ex.1.) var fileNo = "2011-1234567" if (fileNo already exists) { fileNo = 2011-1234567-2 } ex.2.) var fileNo = "2011-1234567-2" if (fileNo already exists) { fileNo = 2011-1234567-3 } ect. Much thanks and appreciation for all your help. Jay Hello, I need your help. For one reason or another, I have a textarea with text in it, however, when I call it back and store it in var, it seems to strip all the spaces. [ID = "COMMENTS"] Ie. of what's in the textarea: ---------------------------------------------- Apples are red Oranges are orange Pears are green Bananas are yellow var comments = document.getElementById("comments").value returns: Apples are red Oranges are orange Pears are green Bananas are yellow How could this be re-written such that the spaces in between the text aren't ignored? Much thanks and appreciation for everyones help. Cheers J Hello, I need your help. How could I write a functional if statement such that it would check against a series of various word strings seperated by commas ie. function check(y) { //y = "FOR INPUT" var string_to_check = FOR INPUT, FOR ACTION, FOR APPROVAL if (y isin string_to_check) { return 1 } } Thanks for everyones help. J I want to Count the number of occurrences of character 't' in a string entered by the user (via prompt) and write the result to the web page. For example, if the user enters "Working with strings", you should write a message saying that the string contains 't' 2 times. I know it has something to do with charAt. Im just playing around with it trying to figure it out and got Code: var str = prompt(); document.write(str.charAt(1)); That doesnt count anything up obviously. i need to use a loop but im not sure how to apply a loop and charAt. I have a Number as a String. It goes from 1 to 999 but I need to set the format so that it has leading Zeros. So 1 is 001 and 2 is 002 and 99 is 099. Is there a way to do this in JavaScript? Thanks, ~Dave Hello, How would I seperate a text string such that it would appear on seperate lines ie. Initial Input: ------------------------------------------- StrMsg = "This is an example of a string that will appear on seperate lines" "Hoping that this fully works, there will be no errors and all will be well" "Thank you to the experts and pros on this forum who are here to help" "This is the last line of text." Expected Outcome: ------------------------------------------- This is an example of a string that will appear on seperate lines Hoping that this fully works, there will be no errors and all will be well Thank you to the experts and pros on this forum who are here to help This is the last line of text. Much thanks and appreciation for all your help J Function findsometing() ( var myString = 'results' + '<BR>' for (var initiator, test condition,incrementor.) { if (.....) { myString ='some quote'; } else if (.....) { myString += something + '<br>' } else if (....... ) { myString += something + '<br>' } else { myString += ''; } } displayMessage(myString) ) I am a complete newbie to javascript and programming, so excuse any faux pas, but I want to check the myString variable after all the loops in the for loop statement have been completed. For example if nothing was added to the var myString after the completeion of the for loop, i want to display a message that no results found. i tried various attempts of an if statement along lines of } } if (myString = 'results') ....i also tried if myString= ('results' + '<BR>') { displayMessage('no results found') } else { displayMessage(myString) } ) Any pointers as to where i am going wrong? Hey all, I have a simple example below showing how when I pass in the value of the value attribute of option node, and then use if operator to check whether parameter is a string or not, even though it's a string, it converts it to false boolean and triggers the else statement rather than calling a function: Code: <body> <select> <option value="createMsg">Add Message</option> <option value="removeMsg">Remove Message</option> </select> </body> Code: var menu = { handleMenu : function(callback){ if(callback === "string"){ menu[callback](); } else { console.log("Issue occurred") } }, createMsg : function(){ var content = document.createTextNode("Please give additional information."), heading = document.createElement("h1"); heading.appendChild(content); document.body.appendChild(heading); }, removeMsg : function(){ } } document.getElementsByTagName('select')[0].onchange = function(){ menu.handleMenu(this.value)}; callback should be a string so why is it saying otherwise? Thanks for response I found a regular expression that will strip my string of every html tag... var stripped = htmlStr.replace(/(<([^>]+)>)/ig,""); Well, I also want it to strip every thing between parentheses, square brackets, and replace " &# 160;" with a space. It would be really cool if you could explain how to do this as well, because the stuff in the above code after "replace" confuses me. I have no idea how that's working. First of all, I don't want to replace text in a form, or on click. I'm writing up a design document in HTML, and I need an area to display code. The < and > tags won't show up, obviously, but to make it easier on the programmer, I don't want him to have to manually put in < and > for each time it shows up. I am aware that JavaScript has a replace function, but I am not familiar with JavaScript quite yet. Is there a way to have JavaScript find the < and > in a certain div, and replace it with < and > on load? I would appreciate your help so much, Its been frustrating me. |