JavaScript - Need A Hand With A Regex In Js
THIS IS NOT HOMEWORK
Hi all, I'm trying to extract a URL from a string that may or may not have extra characters before and/or after the URL. I'm having a heck of a time making the regex work... when it works for one case it fails for another and I'm just not seeing why. OK, here is the code: Code: var str = 'leading junk http://h71036.www7.hp.com/hho/images/Outdoor_scene.jpg trailing junk'; var txt = str.replace(/^\s?((https?|ssh|ftp|mailto|file|www):\/\/.*?)\s?$/gi,'href:\n1 is $1\n2 is $2\n3 is $3'); alert(txt); (eventually the replacement string will be <a href="$1">$1</a> of course) What I want is of course to extract "http://h71036.www7.hp.com/hho/images/Outdoor_scene.jpg" out of the string whether of not there is leading and/or trailing "junk" around the string. Any ideas? I'm stumped... Thanks! -- Roger Similar TutorialsHello, I am trying to create an input box with a submit button as following: - when any random text is entered in the box, upon submit, a new page should open, based on the text entered: for example: if 'test' is entered ... the new page that opens should be 'test.html' if 'monday' is entered ... the new page that opens should be 'monday.html' Thanks in advance Hi, I've been working at a website for quite some time now, one for my portfolio that displays my graphics design talents. However when coding this website I've run into quite a few problems. The most concerning being the photo gallery I am trying to set up inside of my website. I believe the problem I have is that I have two conflicting scripts, (in which case, how do I 'unconflict' them?) I have a sliding image header and I am using lightbox to create my gallery. While the slider works, my images only work as normal links rather than popping up on the screen. If you have any idea on how to fix this, please let me know. I am also open to other possibilities for a photo gallery. I've got the following html code for a button - how can I get the cursor to turn into the hand shape when the mouse is over the button? Thanks PHP Code: <INPUT NAME="btn_cancel" TYPE="button" VALUE="Cancel" onClick="cancel_button_click()";> Hello, I'm not sure what to call it. The only way to describe it is a 'window frame'. I need to increase the size. I've been waiting for days to get any kind of response from the theme developers, ElegantThemes.com. If you look at http://www.woodstock2012.com and you click something from the menu a window opens that is very small that has big borders and heavy padding around it. I need to increase the size of this and adjust some margins and padding. Can anyone give me a hand? I sure would appreciate it. This is the code in my custom.js file...the file the admin told me to adjust. Which I did and it didn't work. This is the original code: He said simply, adjust this code... Code: $et_main_content.css( { top : '77px', width : '800px', height : ( et_window_height - 200 ), marginTop : 0, marginLeft : '-400px' } ).removeClass(); I found this code twice in the file: Code: $et_main_content.css( { top : '77px', width : '900px', height : ( et_window_height - 200 ), marginTop : 0, marginLeft : '-400px' }, and I also found this once: Code: $et_main_content.animate( { top : '77px', width : '800px', height : ( et_window_height - 200 ), marginTop : 0, marginLeft : '-400px' }, I change them to: Code: $et_main_content.css( { top : '27px', width : '900px', height : ( et_window_height - 200 ), marginTop : 0, marginLeft : '-450px' }, and Code: $et_main_content.animate( { top : '27px', width : '900px', height : ( et_window_height - 200 ), marginTop : 0, marginLeft : '-450px' }, The results for this change are as such...if I click to a page from the main 'drop in' menu the page loads wide but the minute I use an arrow to change pages or click a page from the menu at the bottom of the page the size reverts back to the 800px. It doesn't lose the margin change from -400px to -450px but it does lose the width. I also need to know how to make it taller and reach closer to the bottom menu and the content slider needs to be adjusted also. I've attached the full js file. If anyone can help me out here I sure would appreciate it. Thank you in advance. Doug The suspect line is the following $('.leaveClan').attr('id') = <?php echo $id; ?>; the span with the class (there is only one) leaveClan is on the main page, and that line of javascript is on a page being called by AJAX within that page. Can anyone see why that is causing an error? Let me know if you need to see any more of the code Hi, I have written a function to compare each character in startString with another nominated character (nomChar). If they are the same this character is written to outputString, in the same position. If they are not the same a character is lifted from the same position in altString and placed in the same position in outputString instead. I hope that's clear! Code as follows: Code: function compareChar(startString, altString, nomChar) { var outputString = ''; outputString.length = startString.length; var nextLetter = ''; for (var count = 0; count < startString.length; count = count + 1) { nextLetter = startString.charAt(count); if (nextLetter == nomChar) { outputString.charAt(count) = nextLetter; } else { outputString.charAt(count) = altString.charAt(count); } } } document.write(compareChar('boston', 'denver' , 'o' ) ); However, the line of code within the if statement Code: outputString.charAt(count) = nextLetter; keeps generating an 'Invalid assigment left hand side' error message. Can anyone see why this is the case? Thanks. Using onclick=window.open function in js to open a pdf file link in a new popup window. Works fine to display the file onscreen, but not if the user wants to save the file client-side to their computer. The right-hand-button context menu for the mouse will allow the user to download, but the file saved will be a html dump file for the webpage and the name of the file will be that for the webpage. Of course I can use the easy <a href> method for download links and the mouse context menu options will be as expected, but I can only use target="-blank" or target="_self" . I need a popup window to open. Could use : oncontextmenu="alert('Left click the link to open, and then SAVE from with the pdf viewer') to advise users how to save the file, and could use "javascript: void(0)" to eliminate most mouse context menu options, so the user won't bother try. So how can I get a link to a file which can be viewed in a popup window and downloaded using mouse right-hand context menu? Any advice massively appreciated! Hi, I'm using this site Translator Editor - LingoJam I'm trying to use the regex portion of the site (click regex on the page that link goes to) to create a substitution cipher - a translates to o, etc. so far I have Code: /a/g -> o /o/g -> u Theoretically, that'd mean when I typed in "a", it would translate to "o", and when I typed in "o", it would translate to "u". Trouble is, it doesn't know when to stop. I type in "a" and it immediately returns "u", because it substitutes the "o" that it should end up as for the "u". Any help on this matter would be appreciated. Thank you. Reply With Quote 12-29-2014, 08:34 AM #2 Philip M View Profile View Forum Posts Supreme Master coder! Join Date Jun 2002 Location London, England Posts 18,371 Thanks 204 Thanked 2,573 Times in 2,551 Posts That is bound to occur if you use onkeyup to translate the letter immediately it is typed. You should create the cyphered text once the whole message is complete. All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit. I have this code so far; PHP Code: validatePhone : function(field, caption) { phoneNumber = /^[0-9]{11}$/; if(phoneNumber.test(field.val())) { return true; } return i18n('phone', caption); }, It works fine for entering an 11 digit number. However, I want to check that the number starts with either 077, 075, 079 or 078. Also, if the field is empty then it can also be accepted. How would I do this? Thanks Should alert '' twice , instead alerts original text Code: text = 'p18=100'; text = text.replace("(^|&)p100=18.*?(&|$)","") alert(text); text = text.replace(/(^|&)p100=18.*?(&|$)/g,"") alert(text); tested regex on online tester and it should be OK. ? I am creating a userscript and trying to take the following: Arian Foster (Hou - RB) and end up just with the team name: Hou You can't remove by index (which I would be able to do), because it might be K instead of RB or NO instead of Hou. I am assuming that you use Regex and the replace function to do so, but I am struggling with understanding all Regex symbols. Can someone help me with this and explain it briefly? I would appreciate it. I am trying to grab all text between <START> and <END> and have the following bits of code, but neither are returning anything. I use JS so rarely that I can't see where the problems are - Code: var ermtext = response.match(/<START>(.*?)<END>/i); if (ermtext) { result.ermtext = ermtext.replace("$1"); } Code: var patt=/<START>(.*?)<END>/i; result.ermtext = response.match(patt, "$1"); Hello, I have this piece of code I read from a book to remove text nodes that only have white space. Code: if (node.nodeType == 3 && ! /\S/.test(node.nodeValue)){ // code to remove the text node } Why should we use this: Code: ! /\S/.test(node.nodeValue) Instead of this? Code: /\s/.test(node.nodeValue) I am trying to run a simple replace over some css a user puts into a textarea. I would like to remove all the comments Code: /* MY COMMENT */ I have tried the following to try and escape the forward slashes but to no avail. Code: replace(/\/*(.*)\//g,""); any ideas? Thanks! Hey everyone, I'm working on a script, but I've been stopped by an error code from regex's match function. Firefox is giving me a "text.match is not a function". My assumption is that it may be the getSelection that is causing the error. Any clarification would be much appreciated! Code: function captureText(){ var txt = ""; if(window.getSelection){ var text = window.getSelection(); } else if(document.selection){ var text = document.selection.createRange().text; } else { return; } if(text != ""){ var reg = /^[A-Z](.*?)+(\.)$/g; var check = text.match(reg); alert(check); alert("You selected: "+text); if(window.getSelection){ window.getSelection().removeAllRanges(); } else if(document.selection){ text.blur(); document.selection.empty(); } else { return; } } } document.onmouseup = captureText; Thanks. Hi All, i have this Regex which is below and im trying to validate a field when a button is clicked i may have this mest up but what the regex does it checks for 1 decimal place and returns true or false i put some comments in with the code Code: function IsNewBid() { var NewBid = document.getElementById('MainContent_txtNewBid').value; var result; result = ^[0-9]{1,10}[.]{1}[0-9]{1,2}$|^[0-9]{1,10}$(NewBid .Value) if (result 'Equals False So theres and error') Do something else{ carry on } } Any help will be highly appreciated.. Thank you for your time. 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! hi, Getting on quite well, I feel. just would like you to confirm that this regex will strip out characters that cannot be in a phone number those are (imv) 0-9 - ( ) + Code: val = val.replace (/[^0-9\\s\\-\\(\\)\\+]/gi,""); // strip the characters that cannot appear in a phone number I use double backslashes to keep the perl interpreter happy. otherwise (in case it simplifies it for you), it would be Code: val = val.replace (/[^0-9\s\-\(\)\+]/gi,""); // strip the characters that cannot appear in a phone number bazz i'm trying to make a validation page and i am having issues with some of them AGE - i'm not sure where to go from here Code: var age = /^[0-9 ]{3,20}$/; // trying to check age (only 18-25 is valid) Name - how do i make it so that no spaces are allowed? Code: /^[A-Za-z ]{3,50}$/ street - i have no clue where to even begin on this. i think that RegEx can't be used to validate all addresses, but something simple like 123 main street. maybe something like this Code: preg_match('/.{2,60}$/',$address) here's an example of my zip code one to give you an idea of what im going for Code: function checkZip(){ var re5digit=/^\d{5}$/ //regular expression defining a 5 digit number if (document.myform.zip.value.search(re5digit)==-1) //if match failed alert("Please enter a valid 5 digit number inside form") } HTML FORM Code: <form name="myform" onsubmit="return checkZip()"> <fieldset> <legend>Test Form</legend> Firstname*: <input type="text" name="firstname"/><br/> Lastname*: <input type="text" name="lastname"/><br/> Street*: <input type="text" name="street"/><br/> City*: <input type="text" name="city"/><br/> State*: <input type="text" name="state"/><br/> Zip*: <input type="text" name="zip"/><br/> Age*: <input type="text" name="age"/><br/> Gender*: <input type="text" name="gender"/><br/> Major/Program Choice*: <input type="text" name="major"/><br/><br/> <input type="reset" /> <input type="submit" /> what is a good way to combine these into all one function using getElementByID? i could make another function and put all the other functions in it, but i'm not sure where i can incorporate getElementByID when the verification is complete, i want to display all the info using getElementByID and innerHTML. should i make vars that hold all the info and put in after a span somewhere? |