JavaScript - Help With Regex Expression
Hi:
I think this is where I need to ask this. I am doing a PHP/mod_rewrite and I need to tweak one of the RegEx's used in it but I don't know the syntax. I have this bit of code: Code: <a href="http://www.mywebsite.com/Promotional.Products-Promotional.Items/Promotional-Products/<?php echo $product_id ?>-<?php echo str_replace(" ", "-", $myTitle) ?>-<?php echo $full_state ?>-Promotional-Products.html" title="<?php echo $myTitle?> <?php echo "$full_state"; ?>"><?php echo $myTitle?></a> RewriteRule ^Promotional.Products-Promotional.Items/Promotional-Products/([0-9]+)-([a-zA-Z]+)-([a-zA-Z]+)-Promotional-Products.html$ Promotional.Products-Promotional.Items/Promotional-Products.php?product_id=$1&myTitle=$2&full_state=$3 I wanted to know how I can revise the middle RegEx: Code: ...-([a-zA-Z]+)-... to include replacing all spaces with hyphens. Can someone help me with this, please. Thanks! Similar TutorialsI am trying to change the background color of TD's that only the exact text PTO and really do not grasp regex's string/element examples: Code: <td class="ms-cal-todayitem"> Ward, Daniel M. - PTO - home - im awesome. </td> <td class="ms-cal-todayitem">Ward, Daniel M. - UPTO - home - im awesome. </td> I tried this: Code: $("td.ms-cal-todayitem").filter(function() { return /PTO/.test( $(this).text() ); }).css("background-color","#C4DBFF") but it also changes the background of items like my second element example. - Yes it should be case sensitive, if possible. I have the following regular expression to test the validity of an input date: Code: ([0-9][0-9]|[0-9])[\s/-]([0-9][0-9]|[0-9])[\s/-]([0-9][0-9][0-9][0-9]|[0-9][0-9]) It should allow 1 or two digits for the day of the month, then a separating character (" ", "/" or "-"), then 1 or two digits for the month, then 2 or 4 digits for the year. Unfortunately it will accept anything above 2 digits for the year, which I understand why is happening but don't know how to fix! Can anyone help? Thanks! I want to find in all occurrences of #(...) in a string (what is between the parentheses is unknown. At the moment I have Code: str.match(/#\(.*\)/) The problem is, I want to be able to have multiple of these in one string, like "#(hello) good #(world)", but RegExp takes me from the first '#', to the last ')'. I want to allow nested parentheses, so I am thinking that there should be away to not let it look past the second '#'. How would I do this? can anyone explain me in detail what this regular expression mean... each and every part of this sentence: var baseRegex = new RegExp("^(?=.{6,})(?=.*^[A-Za-z]([a-zA-Z0-9_-]+)$)(?=.*[A-Z])"); Dear experts I have following codes Var re=/(\d){4}-(\d){7}/; When I test this string 0300-685706263 It says TRUE But I validate this string exactly 0300-6857062 Please help I am trying to find the correct regular expression (or an alternate) for if a user enter any more than 1-3 digits in the age field of a form it produces an error message. I already have the error set up, just the correct coding to produce the 1-3 digit condition. Thanks, KWIKCUCUMBER. Hi, Can someone fix the following regular expression? It is validating every thing expect YEAR. It only matches first 2 digits for year instead of 4. I want it to be 4 digits for a year. It returns true if I put 12/25/19.I want it to return false for the above date. Code: var RegExPattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/; Thanks Hello, How can i validate string with alphanumeric, space, dash and dot in regular expression ? for the character classes [ ] , if i want to match ,.[] i cannot put them into the square brackets so how to deal with that? what if the characters are . or ! or ." (<-- combined) it fails if the regexp is [.!(.")] which will treat ( as one of the element. also the book javascript: the definitive guide says that (?=p) requires that the following characters match the pattern p, but do not include those characters in the match. However, the browser failed to figure this out (IE8) i.e. "asd:ert".match(/(?=\\w/) returns null thx in advance Hi all, I have this line (part of a larger function that converts a few common BBCodes into HTML) and I don't know how to do one thing. First, here's the line: Code: str.replace(/\[color=(.*?)\](.*?)\[\/color\]/gi,'<span style="color:$1;">$2</span>'); It works fine if I do this (I'll mis-spell "color" to prevent messing up the board): THIS works fine..... [colur=#ff0000]Red Text[/colur] But of course, if I do this: [colur="#ff0000"]Red Text[/colur] It doesn't work. What I need to do is take the $1 variable, AFTER it's captured, but BEFORE it's used, and strip out any quote marks I find. I may want to also strip out, then add the hash sign (#) also, then put it back so it works with or without the #. While I'm at it, I'll also probably detect color as a hex string vs. a keyword (so that both "#ff0000" and "red" work) and probably also support the rgb() method. But first I need to know how to access and modify $1 BEFORE it's used. I'm hoping that I don't need to use two lines (or two separate .replace() calls). Thanks! -- Roger I want to validate a form with only numbers and "$" sign. so far i know this Code: currencyValue = which.value; var re = /^[0-9]*$/; if (!re.test(currencyValue)) { alert("Value must be all numberic charcters, non numeric's removed from field!"); field.value = field.value.replace(/D/g,""); } but how do I also add the "$" sign as ok? I need to find a way to check if a field contains sql commands. To begin with, lets just say as an example:select, delete, drop, truncate <script type="text/javascript"> function validate () { var literals= /select/; user_input = document.forms[0].input1.value.search(literals); if (user_input == -1) { alert("test123"); } } </script> <body> <form > <input type="text" size="25" name="input1" value=" "> <input type="button" onClick="validate()" value="check" /> </form> I am new to JS and regular experssions. How do I make it in my regular expression so not only am I searching for /select/ but also for the other sql commands (or anythign for that matter)... essentially multiple literals... eg if it contains /select OR delete OR truncate/ etc. sorry if this is confusing. let me know if i can help in any other way. thanks guys! Hi guys, May i know how to write this string (1,2,4-6,9,11-13,20) in regular expression? Restrictions:- 1) Only numbers, comma and hyphen are allowed 2) no spaces are allowed Thanks much! I have the following function: Code: function sonKare(sayac, oteki) { var vertical, horizontal = hangiYataySira(sayac, oteki), j, k, l, m, united, tekRakam, actualV = [], actualH = [], numaralar = [1, 2, 3, 4, 5, 6, 7, 8, 9], olmayanlar = []; if(ilkSefer === true) { vertical = hangiDikeySira(rakamlar.length); ilkSefer = false; } else { vertical = hangiDikeySira(rakamlar.length - 1); } for(j = 0; j <= vertical.length - 1; j ++ ) { if(rakamlar[vertical[j]] !== undefined) actualV.push(rakamlar[vertical[j]]); } for(k = 0; k <= horizontal.length - 1; k ++ ) { if(rakamlar[horizontal[k]] !== undefined) actualH.push(rakamlar[horizontal[k]]); } united = actualV.concat(actualH); //---- Olmayan rakamları bulan kısım ------------ outer: for(l = 0; l <= numaralar.length - 1; l++) { for(m = 0; m <= united.length - 1; m++) { if(numaralar[l] === united[m]) continue outer; if(m === united.length - 1 && numaralar[l] !== united[m]) olmayanlar.push(numaralar[l]); } } //---------------------------------------------- if(olmayanlar.length > 1 || olmayanlar.length === 0) { rakamlar = []; document.form1.buton.click(); // THIS LINE DOES NOT WORK SECOND TIME } else { $("_" + oteki + "_" + sayac).value = olmayanlar[0]; rakamlar.push(olmayanlar[0]); sayac++; if(sayac === 10) return; else sonKare(sayac, oteki); //CALLS ITSELF } } This is a recursive function. As stated above there is a line which does not work for the second time. What I mean is, it works perfect for the first time and after function calls itself, it doesn't work. Javascript does execute the line above it but just disregards that document.form1.buton.click(); line. I use Firebug for debugging and I checked it many times, when that expression needs to be executed for the second time, JavaScript just ignores it and immediately goes at the end of the function... I just can't figure out why JS does such a thing? p.s: Don't bother about weird variable names, they are not in English but in Turkish which is my native language The following code removes the brackets and a dash from the string variable. I am interested why there are \ in front of each bracket in the regular expression regexp? The code work fine if regexp would be /()-/g; So why then / is necessary? I would appreciate a lot with someone could explain. Thank you very much. <html> <head> <title>Regular Expressions</title> <script type="text/javascript"> var string="(304)434-5454"; regexp=/[\(\)-]/g; document.write(string.replace(regexp, "")); </script> </head> <body> </body> </html> I have a function that changes the label on a set of labels with similar "for" fields. Somehow, the matching is only matching every other label. I suspect I must be missing something obvious. The goal is to change the text of a set of radio buttons to either "top" and "bottom" or "left" and "right" depending on orientation. Code: function ChangeLabels(orientation) { var lefttop = "Left"; var bottomright = "Right"; var lbls = document.getElementsByTagName("label"); var lefttopregex = new RegExp ("Field6_[0-9]+", "gi"); var bottomrightregex = new RegExp ("Field20_","gi"); if (orientation == "Vertical") { lefttop = "Top"; bottomright = "Bottom"; } var forvalue = ""; for (var i=0,k=lbls.length; i<k; i++) { forvalue = lbls[i].htmlFor; if (lefttopregex.test(forvalue)) { lbls[i].innerHTML = lefttop; } if (bottomrightregex.test(forvalue)) { lbls[i].innerHTML = bottomright; } } } And here is the HTML of the labels. Code: <li id="fo1li6" class=" "> <label class="desc" id="title6" for="Field6_0"> Left/Top <span id="req_6" class="req">*</span> </label> <div> <input id="radioDefault_6" name="Field6" type="hidden" value="" /> <span> <input id="Field6_0" name="Field6" type="radio" class="field radio" value="Dogs" tabindex="20" onchange="handleInput(this);" checked="checked" /> <label class="choice" for="Field6_0" > Dogs</label> </span> <span> <input id="Field6_1" name="Field6" type="radio" class="field radio" value="Pineapple" tabindex="21" onchange="handleInput(this);" /> <label class="choice" for="Field6_1" > Pineapple</label> </span> <span> <input id="Field6_2" name="Field6" type="radio" class="field radio" value="Leaves" tabindex="22" onchange="handleInput(this);" /> <label class="choice" for="Field6_2" > Leaves</label> </span> <span> <input id="Field6_3" name="Field6" type="radio" class="field radio" value="Cat" tabindex="23" onchange="handleInput(this);" /> <label class="choice" for="Field6_3" > Cat</label> </span> <span> <input id="Field6_4" name="Field6" type="radio" class="field radio" value="Star" tabindex="24" onchange="handleInput(this);" /> <label class="choice" for="Field6_4" > Star</label> </span> <span> <input id="Field6_5" name="Field6" type="radio" class="field radio" value="Bamboo & Sun" tabindex="25" onchange="handleInput(this);" /> <label class="choice" for="Field6_5" > Bamboo & Sun</label> </span> <span> <input id="Field6_6" name="Field6" type="radio" class="field radio" value="Bamboo" tabindex="26" onchange="handleInput(this);" /> <label class="choice" for="Field6_6" > Bamboo</label> </span> <span> <input id="Field6_7" name="Field6" type="radio" class="field radio" value="Sun & Moon" tabindex="27" onchange="handleInput(this);" /> <label class="choice" for="Field6_7" > Sun & Moon</label> </span> <span> <input id="Field6_8" name="Field6" type="radio" class="field radio" value="Fleur de lis" tabindex="28" onchange="handleInput(this);" /> <label class="choice" for="Field6_8" > Fleur de lis</label> </span> <span> <input id="Field6_9" name="Field6" type="radio" class="field radio" value="Dragonflies" tabindex="29" onchange="handleInput(this);" /> <label class="choice" for="Field6_9" > Dragonflies</label> </span> <span> <input id="Field6_10" name="Field6" type="radio" class="field radio" value="No Design" tabindex="30" onchange="handleInput(this);" /> <label class="choice" for="Field6_10" > No Design</label> </span> </div> </li> Me again, having some issues with the following code... PHP Code: if (str.match(regEx)) { alert("illegal character"); } regEx = /[^0-9a-zA-z]/g I have tried typing in loads of strings, and nothing is causing the alert to work. Edit: Fixed it, it seems the fix is: regEx = [^0-9a-zA-z] I thought you need the /. I also can't use any modifiers, although it works fine without them. Hi all, As I scan each line of data that was entered into a textarea box, I need to call a function once I come across a line that starts with the following: Code: "0 B3C 0020365077 0045740301 3 09FEB2008 09MAR2010 " (without quotes) I have created the following legend to help with the creating a regular expression that will validate when I have found a line that needs further processing: N - number L - letter A - letter or number S - space So using the above legend, the first 51 characters of the line that I need to validate should always be in the following format: Code: "NSAAASSAAAAAAAAAASNNNNNNNNNNSASNNLLLNNNNSNNLLLNNNNS" (once again, without the quotes) I have been reading a few articles on JavaScript regular expressions but am a bit lost at this point as how to start? I want a regular expression for Citi where first character should not be a blank space. Thanks in advance 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 |