JavaScript - How To Replace 2 Apostrophes In A Sentence?
In my functionality, I need to replace all the apostrophes(') present in the sentence with double apotrophes('') so that it can be stored in the database. I am able to replace the first apostrophe with indexOf() and replace() methods.
How do I replace the other apostrophes present in the string? Similar TutorialsDoes anyone know of an easy way, using javascript/jscript/jquery only, to get the first sentence of any Wikipedia article? I've tried using ajax to grab all the html and then narrow it down using a couple of split() functions, but not every article is the same, so I don't think I can do it that way. Any ideas? I am taking a Javascript class and the teacher assigned this: Quote: Write a script that uses a random number generation to create sentences and name it sentences.html. Use five arrays of strings called: uppercase article (uarticle), noun, verb, lowercase article (larticle), and preposition. You will need to use the correct case for the article arrays. Create a sentence by selecting a word at random from each array in the following order: uarticle, noun, verb, preposition, larticle, noun. You can find examples of generating random numbers in both Fig. 8.6 (dice-rolling) and Fig. 8.7 (random image) of Chapter 8. The arrays should be filled at minimum, as follows: the article array(s) should contain the articles: the, a, one, some and any. The noun array should contain the nouns: boy, girl, dog, town and car. The verb array should contain the verbs: drove, jumped, ran, walked, and skipped. The preposition array should contain the prepositions: to, from, over, under and on. If you would like to add more words, adjust the arrays appropriately. As each word is picked, concatenate it to the previous words in the sentence. The words should be separated by spaces. When the final sentence is output, it should start with a capital letter and end with a period. The program should generate 20 sentences and output them to the screen (document.write or document.writeln). You should use a for loop similarly to the one used in Fig. 8.6 in order to get it to print 20 times. I haven't gotten to the loop yet, I'm just working on the random sentence first. Here is what I have: Code: <script type="text/javascript"> <!-- uarticle = new Array("The", "A", "One", "Some", "Any"); noun = new Array("boy", "girl", "dog", "town", "car"); verb = new Array("drove", "jumped", "ran", "walked", "skipped"); larticle = new Array("the", "a", "one", "some", "any"); preposition = new Array("to", "from", "over", "under", "on"); var rand1 = [Math.floor ( Math.random() * uarticle.length )]; var rand2 = [Math.floor ( Math.random() * noun.length )]; var rand3 = [Math.floor ( Math.random() * verb.length )]; var rand4 = [Math.floor ( Math.random() * larticle.length )]; var rand5 = [Math.floor ( Math.random() * preposition.length )]; document.write(uarticle[rand2] + " " + noun[rand2] + " " + verb[rand3] + " " + preposition[rand1] + " " + larticle[rand4] + " " + noun[rand2] + "."); --> </script> Am I on the right track? How would I loop the sentences using a for statement? please give me the code for checking the company name entered in textbox using javascript .Only sentence case to be allowed.Only abbreviations (without a full name) should not be allowed.(eg DPS) Abbreviations if any should be allowed only at the end of the name within ().eg Delhi Public School(DPS)
i 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. 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. 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 Hi all, I have this code: Code: <input type="hidden" value="update_cart" name="ca"> <input type="hidden" value="2" name="numcart"> <input type="submit" name="submit" value="Update Cart" class="inpSubmit"> I'd like to replace this: Code: <input type="submit" name="submit" value="Update Cart" class="inpSubmit"> with this: Code: <button>blabla<button> How can I do? I know something about unobtrusive javascript but I don't know how to do it. Thank you very much 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? 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/); } Does anyone know if it it possible to automatically replace any instance of & with 'and' using javascript when someone types into a text input?
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>? How would you do a .replace for a Javascript if statment.
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.
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; } 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> document.write(str.replace(/(\+|\(|\)|.|-|\/ /g,'')); feeble attempt to remove '.' '(' ')' ' ' '-' '/'and '+' I am trying to fix some datetime strings so I can convert form values into date objects so I can compare the dates in my validation process. the date time strings look like - 7/21/2011 8:00am for some reason the am is not being replaced by " am" . here's the function Code: function ampm(str){ str.replace(/am/gi," am") str.replace(/pm/gi," pm") return str } for example the result of ampm("8:00am") is 8:00am and not 8:00 am .. it seems so simple. So I have a site, It has a few paragraphs. When a customer presses a href, or a navigation link, I want that block of text to change to a different set of paragraphs. For instance Ive got a Div set for the main block of text with general information. When they click a link at the top, I want this text to change to something else, without having to reload the entire page or create a completely new HTML file. I have read on innerhtml and setting particular elements but I cant find any good online resources for using a simple link rather than a button to change a block of text In essence Set block of text to be element "old text" Set block of new text to be element "new text" ( all the while not showing this text on the page until the link is pressed) Classify href as trigger to replace old text with new text. BAM, victory! Any help would be greatly appreciated. |