JavaScript - Replace String With Asterisk
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. 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. 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. 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. 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; } 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? OK I've been stuck on this problem for like 4 days already, so I'm resorting here. This is my problem Code: (A) (B) (C) (D) * ********** ********** * ** ********* ********* ** *** ******** ******** *** **** ******* ******* **** ***** ****** ****** ***** ****** ***** ***** ****** ******* **** **** ******* ******** *** *** ******** ********* ** ** ********* ********** * * ********** Now im suppose to write the script that makes patterns A-D stack on top of each other, so A on top, B under it, then C, and then D. I managed to get A and B with not many problems, but when it comes to C and D, I'm just confused on how to get them spaced over. Apparently I'm suppose to use the Pre tag, but im still not sure how to do that either. Im not asking for anybody to do this for me, all I need is a little guidance on how to start C and D. Here is what i have for A and B. I also did one for C, but as i said before, not sure on how i would have it space out in the loop. If you see any mistakes or have a better way of writing this, let me know please. Any help is appreciated. Thank you. Code: <script type = "text/javascript"> nextRow: for (var row = 1; row <= 10; ++row ) { document.writeln( "<br />" ); for ( var column = 1; column <= 10; ++column ) { if ( column > row ) continue nextRow; document.write( "*" ); } } nextRow: for (var row = 1; row <= 10; ++row ) { document.writeln( "<br />" ); for ( var column = 10; column >= 1; --column ) { if ( column < row ) continue nextRow; document.write( "*" ); } } nextRow: for (var row = 1; row <= 10; ++row ) { document.writeln( "<br />" ); for ( var column = 10; column >= 1; --column ) { if ( column < row ) continue nextRow; document.write( "*" ); } } </script> 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 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 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. 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. Hi I'm looking for a very simple script to replace one div with another. I've found a few but they seem overly complicated with features I don't need. I simply want to be able to click on a div and for it to be replaced with another div previously hidden. If someone could suggest a solution or point me in the right direction I'd really appreciate it. Thanks in the div class "done" it reads 13/34 i want to compare these to numbers like this if (13 < 34){do something()} else {return false;} so i am trying to get each number by it self to compare with. var did=m[1].getElementsByClassName("done")[1].innerText.replace(/\/.*/,"").value; this line returns 13 correctly. var need=m[1].getElementsByClassName("done")[1].innerText.replace(? ,"").value; this ? the info that i need to get for the other number. i know how to do it if it were a letter but can find the correct way being that it is a / instead. Thx I have a javascript that takes information from a form and recompiles that information into a readable shift report. The problem I am having is with returns in the textareas. I tried: Code: theReport = theReport.replace(" ","<br>" but that space puts the rest of the var on a different line and screws with the results. How can I replace a return with <br>? Hi, I need function for replacing one string with some html tag (for example <div>something</div>) in web page. Tricky is that in this replacing I must ignore all text between <a> and </a>. It's look easy but I am not good in javascript.
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? Building a shopping cart, at the moment I have 1 variable array. My JS gets values from hidden inputs when add to shopping cart is clicked. The JS then pushes them all into the array in order. I've managed to get the js code to replace innerHTML and add the array to the HTML to show the products, quantity and totals in a table. However its only showing the last product, price, and total in the array in the table. I need to get it to do this: Product: Quantity: Total: Product1 1 1 Product2 2 2 Product3 3 3 So basically I think I need to have my 3 for statements then plus 2 to each i instead of i++ to print out the products but that still doesn't solve my problem with printing every product in a vertical list, same with quantity and same with total. Heres the code Code: var cart= new Array(); function readInput(){ var prd=document.test.prd.value; var prc=document.test.prc.value; var qty=document.test.amount.value; var total= prc * qty; cart.push( prd ) cart.push( qty ) cart.push( total ); var lenp=cart.length; for(var i = 0; i<lenp-2; i++ ) { document.getElementById("product").innerHTML = cart[i]; } for(var i = 0; i<lenp-1; i++ ) { document.getElementById("qty").innerHTML = cart[i]; } for(var i = 0; i<lenp; i++ ) { document.getElementById("total").innerHTML = cart[i]; } } Code: <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <link rel="stylesheet" type="text/css" href="css\basic.css" /> <script language="JavaScript" src="test.js"></script> <title>Shopping Time</title> <body> <form name="test"> <input type="hidden" name="prd" value="Paper"> <p class="desc" value="Paper"> Product: Paper 1</p> <p class="desc"> <input type="hidden" name="prc" value="1.99"> Price: £1.99</p> <p class="desc"> Select Quantity: <select name="amount" onchange="quantity()"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select></p> <p class="submit"> <input type="button" onclick="readInput();" value="Add To Cart" /> </p> </form> <table border="1px solid black" rules="ALL" frame="void" class="cart" width="100%"> <tr> <th> <p class="cart">Product:</p> </th> <th> <p class="cart">QTY:</p> </th> <th> <p class="cart">Total:</p> </th> </tr> <tr> <td class="test"> <div id="product"> <p class="cart">Product:</p> </div> </td> <td> <div id="qty"> <p class="cart">QTY:</p> </div> </td> <td> <div id="total"> <p class="cart">Total:</p> </div> </td> </tr> </table> </body> </html> Code: dropDownPart.innerHTML = tmpInnerHTML; how does this tmpInnerHTML gets filled : Code: for (var i = 0; i < arr.length ; i++) { var arr1 = arr[i].split('|'); id_tezaver = arr1[0]; term = arr1[1] // if arr1[1] contains ', then ewerything after that disapears inside term, // like "bird's eyes" becomes "bird" // how to handle that ? // bad example how I get to verify that the stuff is complete inside array // term = arr1[1].replace(/'/i, /oo/); } document.write(str.replace(/(\+|\(|\)|.|-|\/ /g,'')); feeble attempt to remove '.' '(' ')' ' ' '-' '/'and '+' Building a javascript shopping cart for an assignment. First time using Javascript so ignore the ****ness of it please Anyway basically I need to replace the qty of an item in an array if the item has already been added to the array. if it hasn't then it adds the normal values to the array. So I assume atm that I have to run an if statement when the function Code: function readInput(prd, qty, prc){ is called that checks in the array before pushing the new values into the array if array contains product name then var id = productarrayID+1 then replace that with the new quantity read in by the form. Quantity in my array is after the product name so I assume productarrayID+1 would give me the qty id variable. So heres the steps, be they correct: 1) get product name from HTML. 2) check if array contains product name. 3) continue with adding to array if product name doesn't exist in array. 4) if array contains product name retrieve id of product name within array. 5) + 1 to the ID to the product name thus giving me the ID for the quantity. 6) run some array.replace with the new id and replace with the quantity. Just wondering if thats a year or no to how that would run. Il post my HTML code but please dont post solutions I would prefer to write the code myself but just checking if the step-by-step process would work thankyou. HTML: Code: <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <link rel="stylesheet" type="text/css" href="css\basic.css" /> <script language="JavaScript" src="js/basic.js"></script> <title>Shopping Time</title> <body> <div id="wrapper"> <div id="header"> </div> <div id="content"> <div id="items"> <div id="column"> <img src="img/paper1.jpg"> <form name="test"> <input type="hidden" name="prd" value="Paper"> <p class="desc" value="Paper"> Product: Paper 1</p> <p class="desc"> <input type="hidden" name="prc" value="1.00"> Price: £1.00</p> <p class="desc"> Select Quantity: <select name="amount" onchange="quantity()"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> </p> <p class="submit"> <input type="button" onclick="readInput(prd.value, amount.value, prc.value);" name="test" value="Add To Cart" /> </p> </form> <img src="img/paper1.jpg"> <form name="test"> <input type="hidden" name="prd" value="Paper2"> <p class="desc" value="Paper2"> Product: Paper 2</p> <p class="desc"> <input type="hidden" name="prc" value="1.00"> Price: £1.00</p> <p class="desc"> Select Quantity: <select name="amount" onchange="quantity()"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> </p> <p class="submit"> <input type="button" onclick="readInput(prd.value, amount.value, prc.value);" name="test" value="Add To Cart" /> </p> </form> <img src="img/paper1.jpg"> <form name="test"> <input type="hidden" name="prd" value="Paper3"> <p class="desc" value="Paper"> Product: Paper 3</p> <p class="desc"> <input type="hidden" name="prc" value="1.00"> Price: £1.00</p> <p class="desc"> Select Quantity: <select name="amount" onchange="quantity()"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> </p> <p class="submit"> <input type="button" onclick="readInput(prd.value, amount.value, prc.value);" name="test" value="Add To Cart" /> </p> </form> </div> <div id="column"> <img src="img/paper1.jpg"> <form name="test"> <input type="hidden" name="prd" value="Paper4"> <p class="desc" value="Paper4"> Product: Paper 4</p> <p class="desc"> <input type="hidden" name="prc" value="1.00"> Price: £2.00</p> <p class="desc"> Select Quantity: <select name="amount" onchange="quantity()"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> </p> <p class="submit"> <input type="button" onclick="readInput(prd.value, amount.value, prc.value);" name="test" value="Add To Cart" /> </p> </form> <img src="img/paper1.jpg"> <form name="test"> <input type="hidden" name="prd" value="Paper5"> <p class="desc" value="Paper5"> Product: Paper 5</p> <p class="desc"> <input type="hidden" name="prc" value="1.00"> Price: £1.00</p> <p class="desc"> Select Quantity: <select name="amount" onchange="quantity()"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> </p> <p class="submit"> <input type="button" onclick="readInput(prd.value, amount.value, prc.value);" name="test" value="Add To Cart" /> </p> </form> <img src="img/paper1.jpg"> <form name="test"> <input type="hidden" name="prd" value="Paper6"> <p class="desc" value="Paper6"> Product: Paper 6</p> <p class="desc"> <input type="hidden" name="prc" value="1.00"> Price: £1.00</p> <p class="desc"> Select Quantity: <select name="amount" onchange="quantity()"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> </p> <p class="submit"> <input type="button" onclick="readInput(prd.value, amount.value, prc.value);" name="test" value="Add To Cart" /> </p> </form> </div> <div id="column"> <img src="img/paper1.jpg"> <form name="test"> <input type="hidden" name="prd" value="Paper7"> <p class="desc" value="Paper5"> Product: Paper 7</p> <p class="desc"> <input type="hidden" name="prc" value="1.00"> Price: £1.00</p> <p class="desc"> Select Quantity: <select name="amount" onchange="quantity()"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> </p> <p class="submit"> <input type="button" onclick="readInput(prd.value, amount.value, prc.value);" name="test" value="Add To Cart" /> </p> </form> <img src="img/paper1.jpg"> <form name="test"> <input type="hidden" name="prd" value="Paper8"> <p class="desc" value="Paper8"> Product: Paper 8</p> <p class="desc"> <input type="hidden" name="prc" value="1.00"> Price: £1.00</p> <p class="desc"> Select Quantity: <select name="amount" onchange="quantity()"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> </p> <p class="submit"> <input type="button" onclick="readInput(prd.value, amount.value, prc.value);" name="test" value="Add To Cart" /> </p> </form> <img src="img/paper1.jpg"> <form name="test"> <input type="hidden" name="prd" value="Paper9"> <p class="desc" value="Paper9"> Product: Paper 9</p> <p class="desc"> <input type="hidden" name="prc" value="1.00"> Price: £1.00</p> <p class="desc"> Select Quantity: <select name="amount" onchange="quantity()"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> </p> <p class="submit"> <input type="button" onclick="readInput(prd.value, amount.value, prc.value);" name="test" value="Add To Cart" /> </p> </form> </div> </div> <div id="sidebar"> <table border="1px solid black" id="table" rules="ALL" frame="void" class="cart" width="100%"> <tbody> <tr> <th> <p class="cart">Product:</p> </th> <th> <p class="cart">QTY:</p> </th> <th> <p class="cart">Price:</p> </th> <th> <p class="cart">Total:</p> </th> </tr> </tbody> </table> <button onclick="viewshoppingcart('table')" class="button">Update Shopping Cart!</button> </div> </div> <div id="footer"> </div> </body> </html> Script: Code: var cart= new Array(); var down= 0; function readInput(prd, qty, prc){ down = down + 1; var total= prc * qty; cart.push( prd ) cart.push( qty ) cart.push( prc ) cart.push( total ); } var p = 0; var q = 1; var t = 2; var s = 3; function viewshoppingcart(id){ if(!cart[p])return; for(var i = 0; i < down; i++ ) { var tbody = document.getElementById(id).getElementsByTagName("tbody")[0]; var row = document.createElement("tr") var data1 = document.createElement("td") data1.appendChild(document.createTextNode( cart[p] ) ) var data2 = document.createElement("td") data2.appendChild (document.createTextNode( cart[q] )) var data3 = document.createElement("td") data3.appendChild (document.createTextNode( cart[t] )) var data4 = document.createElement("td") data4.appendChild (document.createTextNode( cart[s] )) row.appendChild(data1); row.appendChild(data2); row.appendChild(data3); row.appendChild(data4); tbody.appendChild(row); p = p + 4; q = q + 4; t = t + 4; s = s + 4; } down = 0; } |