JavaScript - Help W/ Regular Expressions, Find And Replace Problem
I have a string of code like such:
Code: <font color=black>0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000</font><br><font color=black>0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000</font><br><font color=black>0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000</font><br><font color=black>000000000000000000000000000000000000000000000</font><font color=white>0000</font><font color=black>000000000000000000000000000000000000000000000</font><br><font color=black>000000000000000000000000000000000000</font> except much longer, like... 4000 lines longer. What I want to do is go through this code and replace all the zeros between <font color=black> and </font> with 1's and leave anything between <font color=white> and </font> as 0's. I just really need the regular expression not the code. I started reading a book on regular expressions and it's like... 300 pages, so it would be nice to solve this problem now, rather than have to wait to finish the book to solve this. Hopefully when I'm done reading it will make sense, but can someone help me in the meantime? Similar TutorialsHi I have finally figured how to search a text area for URL's... as below, then reformat so they are clickable links. It works!! But it looks so so messy, is there a better way to do this?? Code: var textNodeLinks = document.getElementById("comments"); textNodeLinks.innerHTML = textNodeLinks.innerHTML.replace(/(^|[\n ])([\w]*?)((www|ftp)\.[^ \,\"\t\n\r<]*)/g, "$1<a href=http://$2$3\ rel="nofollow" target=_blank>$2$3</a>"); textNodeLinks.innerHTML = textNodeLinks.innerHTML.replace(/(^|[\n ])([\w]*?)((ht|f)tp(s)?:\/\/[\w]+[^ \,\"\n\r\t<]*)/g, "$1$2<a href=\"$3\" rel="nofollow" target=_blank>$3</a>"); Cheers Can someone please explain the following code to me!? I don't get regular expressions and how they work :S Code: function trim(string){ return string.replace(/^\s*/, "").replace(/\s*$/, ""); } Thanks! Hello. So I have been doing some regex in php. In Javascript i need to parse a url and replace ALL the & with \/%26 (or what ever that one is) and replace ALL the + with the % thingy. But my problem is I am doing url.replace(/&/,'\/%26') but what is happening is it is replace only the FIRST &. How can I do it to replace ALL &? I was thinking of doing some type of loop but for some reason nothing is working...THANKS IN ADVANCE! Dear Experts I have folloiwng codes Code: <html> <head> <body> <center> <script language="JavaScript"> function checkpostal(){ var re=/^\D{A-Z}$/ if (myform.myinput.value.search(re)==-1) { alert("Good") }else{ alert("Bad") } } </script> <form name="myform"> <input type="text" name="myinput" size=15> <input type="button" onClick="checkpostal()" value="check"> </form> </center> </body> </html> With Onclick, I want to test input through regular expression If entered values is between a-z or A-Z then it must display alert("Good") else alert("Bad") Please help Hi everyone, I am trying to validate a date by using regular expressions. I have parts working, such as only accepts numbers, but I cannot get the range correct. On the "mm" field, I am getting errors. Code: if(form.mm.value=="") { alert("Please insert your birth month"); return false;} var re=/^(0[1-9]|1[0-2]){2}$/; if(!re.test(form.mm.value)) { alert("Please fix birth month!"); return false; } { if(form.dd.value=="") { alert("Please insert your birth day"); return false;} var re=/^\d{2}$/; if(!re.test(form.dd.value)) { alert("Please fix birth day!"); return false; } { if(form.yyyy.value=="") { alert("Please insert your birth year"); return false;} var re=/^\d{4}$/; if(!re.test(form.yyyy.value)) { alert("Please fix birth year!"); return false; Any suggestions will be greatly appreciated. How can I get price from a string using regular expression ? Or by any other means . Let's say. var str="Our shop receives only $. This shoe costs $200. We don't accept anything else then $"; How could I retrieve '$200' from this above string. By any means. Thank you... Hey everyone I am trying to match an email string that ends exactly with ".com". Here's what I have Code: var email = window.prompt("Enter your email", ""); var email_match = /[a-zA-Z1-9-_.]{3,}\@[a-zA-Z]{3,}\.(com)/ if (email_match.test(email)) alert("Email is Valid"); else alert("Email Invalid!"); the (com) also matches commm for some odd reason. What must I change in the code so that only emails ending with .com is valid? Thanks! Hi, i'm new to javascript, im managing to find out how to do most things, but i need to validate two paths. I want to know how to check whether path1 is a subpath of path2. e.g. 1: /files/stuff/mypics/anotherfolder/etc 2:/files/stuff/mypics i want to be able to return a boolean given whether i can move folder 1 in to folder 2 in this case yes. but if you swap them its a no. please help. oxide Hi All I need help with javascript regular expressions. I have a text box in which user can enter only 4 types of values. 1) Varchar(n) where 0 < n <=80. Ex: Varchar(20) (Varchar(n) . 0< n<= 80 means he can enter values onlly like varchar(1) or varchar(2) ..... varchar(80) ) 2) Char(n) where 0 < n <=80. Ex: Char(20) 3) numeric(x,y) where x>y and 1<= x <=35 and 0<= y <=15. Ex: numeric(20,6) If the text box value doesnot match this pattern, we should throw him alert message. Thanks in Advance Hi I've got two examples of using regular expressions one works and the other doesn't but they using exactly the same pattern and the same text only difference is - The example that works uses a form to get the patern and text, the other has the pattern hard coded. Any help will be greatly appreciated Works - Code: <html> <head> <title></title> <style type="text/css"> </style> <script type="text/javascript"> <!-- function frmTestClick() { var re = new RegExp(document.frmTest.regex.value,"gi"); if (document.frmTest.subject.value.match(re)) { alert("Successful match"); } else { alert("No match"); } } function demoShowMatchClick() { var re = new RegExp(document.frmTest.regex.value,"gi"); var m = re.exec(document.frmTest.subject.value); if (m == null) { alert("No match"); } else { var s = "Match at position " + m.index + ":\n"; for (i = 0; i < m.length; i++) { s = s + m[i] + "\n"; } alert(s); } } function demoReplaceClick() { var re = new RegExp(document.frmTest.regex.value, "gi"); document.frmTest.result.value = document.frmTest.subject.value.replace(re, document.frmTest.replacement.value); } //--> </script> </head> <body> <div id="content"> <form name="frmTest" id="frmTest" action="" method="post"> <p>Regexp: <input type="text" name="regex" value="</?(b|strong|font|span)\b(.|\n)*?>" size="50"></p> <p>Subject string: <input type="text" name="subject" value="<font>the quick brown fox jumped over <span>the</span> lazy dog</font>" size="50"></p> <p><input type="button" value="Test Match" onclick="frmTestClick()"> <input type="button" value="Show Match" onclick="demoShowMatchClick()"></p> <p>Replacement text: <input type="text" name="replacement" value="replaced" size="50"></p> <p>Result: <input type="text" name="result" value="click the button to see the result" size="50"></p> <p><input type="button" value="Replace" onclick="demoReplaceClick()"></p> </form> </div> </body> </html> Doesn't work??? Code: <html> <head> <title></title> <style type="text/css"> </style> <script type="text/javascript"> <!-- function stripHTML(sVal,bIgnoreBold){ var sPat="</?(b|strong|font|span)\b(.|\n)*?>",re = new RegExp(sPat,"gi"); if (sVal.match(re)) { alert("Successful match"); } else { alert("No match"); } return sVal.replace(re,""); } //--> </script> </head> <body> <div id="content"> <form name="frmTest" id="frmTest" action="" method="post"> <label>Body: </label><textarea name="txtBody" id="txtBody" rows="10" cols="60"><font>the quick brown fox jumped over <span>the</span> lazy dog</font></textarea><br><br> <input type="button" name="btnDoStrip" id="btnDoStrip" value="Do Strip" onclick="this.form.txtBody.value=stripHTML(this.form.txtBody.value,true);"> <input type="reset" name="btnReset" id="btnReset" value="Reset"> </form> </div> </body> </html> Cheers Al Hello everyone, I'm new to using regular expressions with javascript. I'm trying to search a string for instances of a substring with a particular prefix, followed by some numerical digits. So for instance my larger_string is: "This is a string ABCD12 EFGH124 ABCD76 EFGH90" And initially I want to find all substrings starting 'ABCD', with two following numerical digits. I can do it like this: var answer = larger_string.match(/ABCD\d\d/); But I'd really like to pass the 'ABCD' part in as a string itself, myString, as I'd then like to set myString to 'EFGH' and others, to repeat the search for those. I'm struggling with the syntax for creating a regular expression froma string. So I can do this: var reg = new RegExp("ABCD", "g"); but, this doesn't work: var reg = new RegExp("ABCD\d\d", "g"); and similarly I can do this: var myString = "ABCD"; var reg = new RegExp(myString, "g"); but, this doesn't work: var myString = "ABCD\d\d"; var reg = new RegExp(myString, "g"); What is it that I'm doing wrong here? Thank you Hi wise javascript gurus, I am working on a small form that on the click of a button, it calls a function to validate a string that consists of 3 portions delimited with a dash("-") in the following format XXX-NNNN-XXX where X are letters only where N are numbers only I have to use regular expressions to validate the first portion and verify that its exactly 3 characters long that are letters only if the first portion or "plant code" is larger then 3 characters long an alert gets display also if the first portion contains alphanumeric or numeric characters an alert gets displayed, the first portion has to be in letters only. down below is my code that is not functional at the time because I know for sure the way I check for regular expressions is a bet washy but thats the only thing I was able to come up with, any help will be appreciated. thanks for making time to read my thread Code: <html> <head> <script type="text/javascript"> function validateit() { var number = document.myform.product_number.value; var charnum = number.length; if (number =="") alert("you must enter a product number, field cannot be empty"); else if (charnum < 12 || charnum > 12) alert("the product numbre has to be exactly 12 characters long"); else validate_number(number); } function validate_number(num) { var objRegEx = /^[a-z]+$/; split=num.split("-",3); if (split[0].length!=3) alert("the Plant Code portion of the Product Number has to be only three charactes long") if!(objRegEx.test(split[0]) alert("the Plant Code portion of the Product Number has to be letters only") } </script> <title>waldo's</title> <style type="text/css"> .heading{font-weigth:bold;text-align:center; font-family:"comic sans MS"} </style> </head> <body><form name="myform"> <table align="center" border="1" width="55%"> <caption style="font-family:'comic sans MS';font-size:26pt"> Waldos's WareHouse</caption> <tr class="heading"><td>Product Number</td><td>Plant Code</td><td>Production Run</td><td>Shift Number</td></tr> <tr><td><input type="text" name="product_number"></td><td><input type="text" name="plant_code" readonly></td><td><input type="text" name="production_run" readonly></td><td><input type="text" name="shift" readonly></td></tr> <tr><td><input type="button" value="Check Validity" onclick="validateit()"/></td><td align="right">Validation Date:</td><td colspan="2"><input type"text" size="47" readonly></td></tr> </table></form> </body> </html> Hey I'm trying to make a script which will check if the string which the user sent from a form contains ONLY letters. The problem is if the user entered something like this "25 years old" it allow that to be submitted. It only blocks submissions of the form if the user submits NO letters like this "12345". I want it to block submissions if at least one character isn't a letter. Here's my code: Code: var message = document.myform.formtest.value; var allowed = /[A-z+]/; if(!allowed.test(message)) { alert("The field only allows letters (a-z)."); return false; } Im sorry im tired and cant think how to put this. how do i make \\1 and \\2 = margin-bottom[^;]+); and margin-top[^;]+); Sorry again, Regards. Code: var regep = /margin-bottom:([^;]+); margin-left:0px; margin-right:0px; margin-top:([^;]+);/; elementCSS = elementCSS.replace( regep , "margin-bottom:\\1; margin-left:auto; margin-right:auto; margin-top:\\2;"); 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, ""); I want to change my html depending on what browser the user is using. I know the regular expression works as I've tested it endlessly. Can anyone help? Here is the code <script language="JavaScript" type="text/JavaScript"> if(navigator.appName == "Microsoft Internet Explorer") { function replaceit(s) { s.replace(/.*data=\"(.+?).svg".*/g, " <!-- end .content --> <div class=\"content\" style=\"text-align: center;height: 1000px;\"><img src=\"$1.html\" width=\"1104\" height=\"111\" alt=\"Gastroenterology Book\" /></div>"); } } </script></head> Hi guys I'm trying to make a regular expression that will search for the math operator "-" and not a negative "-". But I can't seem to get it right, something since what i'm looking for is "-" with a whitespace following it. Code: [\+\(-\s)\*\/] But when i run this it does not register at all. Is this the right idea? I'm trying to convert a line using a regular expression. The re I am using is this: <div><span>·<span>[\s*]<\/span><\/span>([^<]*)<\/div> I'm using the expression in the replace function like this: html = html.replace( /<div><span>·<span>[\s*]<\/span><\/span>([^<]*)<\/div>/gi,'<li>$1</li>') ; the line of code I am changing is like this: <div><span>·<span> </span></span>Illness - where a pupil is too ill to leave the house</div> eg it should strip out the code and leave <li>Illness - where a pupil is too ill to leave the house</li> it works fine here http://www.regular-expressions.info/...ptexample.html but when used with the replace command it does not? Can anyone see the problem? (I'm trying to convert bullet points from word) Darren Hello, I am trying to parse a string using regular expressions. This string can potentially have any number of non-alphanumeric characters acting as delimiters. These delimiters can also be grouped together. Here are some examples of the data: 00923:5342:123 --> I want to extract 00923, 5342 and 123 into an array 08765::764375 --> parse this as 08765 and 764375 into an array 3246//672354//23458 --> parse as 3246, 672354 and 23458 23564-73654 --> etc I'm using the following code: Code: var ids = str.match(/\w*/g); But it just returns the first token from each line and ignores the rest. So with the above data I'm only getting back 00923, 08765, 3246 and 23564 into the first element of the "ids" array. I've also tried an "inverse" approach using the split method like so: Code: var ids = str.split(/\W*/g); and I just get the same result - the first token from each line is only returned. My test code looks like this ('str' contains a string read in from a file): Code: var ids = str.match(/\w*/g); //var ids = str.split(/\W*/g); task.logmsg("the length of the array is " + ids.length); for (i = 0; i < ids.length; i++) { task.logmsg("ids=[" + i + "]=" + ids[i]); } I can confirm that ids.length is returning 1 (??) The 'task' object comes from a separate library and the 'logmsg' method just writes to the console. My reptilian brain is struggling with reg exps. This shouldn't be difficult to do, but I just can't get it to work. If anyone out there can lend some assistance, it would be greatly appreciated. Thanks, JD |