Regular Expression matching either a single special character OR a particular sequence

Not sure if I'm doing this right:

/(https?:\/\/\S+)(\s|(& nbsp;))?/g;

This should match a URL beginning with http(s):// and ending with a space character or a & nbsp;

So the problem is this part:

(\s|(& nbsp;))?

That should mean: match either a white space or a & nbsp; but it doesn't work. It never matches for a & nbsp; and just continues until it finds a white space.

I'm not looking for any other http regexp, I'm not looking for a javascript library solution, I'm happy with this, I just want to figure out that last portion.

Edit: some kind of bug in the code formatting on this site, there isn't a space between & and nbsp; but this site turns it into a space if I get rid of that separating space.

-------------Problems Reply------------

The \S+ bit is greedy, and will match as many non-space characters as possible, including any   that might be there. Change it to it's ungreedy equivalent \S+?, and you'll probably have better luck:

/(https?:\/\/\S+?)(\s| |$)/g;

(Updated because I overlooked the trailing ?.)

Category:javascript Views:0 Time:2009-06-20

Related post

  • Regular expression for Alphabet, numeric and special character 2011-09-22

    Can someone please give regular expression for a word, which should have: length in the range of 6 to 10 It must be a combination of alphanumeric, numeric and special character (non-alphanumeric character) --------------Solutions------------- So in o

  • Regular expression - match character, digits and special characters 2011-02-08

    I have the following string: test123 test ödo 123teö"st 123 m.1212 123t.est I only want to match strings as a whole that have either characters, digits and special character mixed together. So the regex should match the following string of the exampl

  • What does this if statement containing very similar regular expression matches do? 2009-11-11

    What does this line of Perl mean? if (/ile.*= (\d*)/ || /ile.*=(\d*)/ ) { I am particularly interested in what the "/ile" means, and why both sides of the || are identical. --------------Solutions------------- The syntax /.../ contains a regular expr

  • What regular expression matches this pattern: 22-NOV-09 2009-12-06

    I have tried: /^([0-3][0-9])-(A-Za-z)-([0-1][0-9])?$/ and am not having success. What am I doing wrong? Thank you folks! --------------Solutions------------- /^([0-3][0-9])-([A-Z]{3})-([0-1][0-9])$/ EDIT : /^(0[1-9]|[1-2][0-9]|3[0-1])-([A-Z]{3})-(0[1

  • java regular expression matching 2010-08-17

    What is the regular expression that can match the following 2 strings. Hi<Dog>Hi and <Dog> in a given text. Update: What regex will match this one? <FONT FACE="Verdana" SIZE="16" COLOR="#0B333C" LETTERSPACING="0" KERNING="0">If you

  • regular expression to find the longest recurring character sequence in a line 2010-12-02

    How do I write a regular expression to find the longest recurring character sequence in a line? --------------Solutions------------- You can do the following regular expression to find repeating characters: (.)\1+ but should use your programming lang

  • Regular expressions: Matching filenames starting with dot (.)? 2011-01-09

    I am a complete Reg-exp noob, so please bear with me. Tried to google this, but haven't found it yet. What would be an appropriate way of writing a Regular expression matching files starting with a dot, such as .buildpath or .htaccess? Thanks a lot!

  • Can someone explain why this regular expression matches differently with different regex flavors? 2011-03-10

    The text is: Here is some text! The regular expression (looking for a single match) is: Here is (\w+\s?)+ In .NET there is one match: Here is some text ...but in javascript there are two: Here is some text and text. Why are there two matches in js an

  • F# Mapping Regular Expression Matches with Active Patterns 2011-04-16

    I found this useful article on using Active Patterns with Regular Expressions: The original code snippet used in the article was this: open System.Text.RegularExpressio

  • Can the order of regular expression matching be guaranteed in .NET? 2011-08-02

    Can the order of regular expression matching be guaranteed in .NET? If I have a string: +Apple+ +Orange+ +Banana+ +Foo+ +Bar+ +Second Line+ +More Stuff+ +Etc...+ Given the pattern (with global dot-all): \+(.*?)\+ Does the RegEx class in .NET guarante

  • How to implement regular expression assertions/lookaround (i.e. \b style word boundary) using a DFA regular expression matcher 2012-04-08

    I would like to implement "word boundary" matches within a DFA based regular expression matcher. Can someone tell me how this is done? To give some background, I'm currently using the "dk.brics.automaton" library, but it does not support assertions (

  • Regular expression: match start or whitespace 2009-02-08

    Can a regular expression match whitespace or the start of a string? I'm trying to replace currency the abbreviation GBP with a £ symbol. I could just match anything starting GBP, but I'd like to be a bit more conservative, and look for certain delimi

  • How do I avoid the implicit "^" and "$" in Java regular expression matching? 2009-03-02

    I've been struggling with doing some relatively straightforward regular expression matching in Java 1.4.2. I'm much more comfortable with the Perl way of doing things. Here's what's going on: I am attempting to match /^<foo>/ from "<foo>

  • Regular Expression matching floating point numbers not ending with 2010-07-28

    I'm trying to extract CSS positions from a string - example: "top left bottom right center 0 0% 50% 100% 360deg 15.5% -16.3%" The problem is that the string also contains an angle "360deg". I don't want any numbers followed by "deg" to be matched by

  • about python regular expression match from right direction 2010-10-17

    normally,we use regular expression match from left to right direction,i want to know is there some switch can match from the right to left in python? or in any other language has this feature embedded e.g. abcd1_abcd2 if give a abcd regular expressio

  • regular expression match php 2010-10-19

    I need to do a regular expression match in php . the case is that . 2 digits before decimal and 2 digits after decimal. the problem is that it may and may not have decimal and after decimal digits . how do i write a expression for this ? ------------

  • How do I figure out which regular expression matches my string? 2011-03-09

    I have 10 regular expressions and 1 one string which may or may not match exactly one of those 10 regular expressions. Does anyone know of a simple way to figure out which regular expression, if any, matches my string? I know that I can simply test e

  • 2-step regular expression matching with a variable in Perl 2011-11-01

    I am looking to do a 2-step regular expression look-up in Perl, I have text that looks like this: here is some text 9337 more text AA 2214 and some 1190 more BB stuff 8790 words I also have a hash with the following values: %my_hash = ( 9337 => 'A

  • Regular Expressions matching in Java 2012-02-03

    I am trying to generate a regular expression match in Java, that accepts all numbers that contain 3 and 7 (in any order, ie there should be at least one 7 for all 3's and vice versa) over the set of integers. So far, I have written the code below, ho

Copyright (C), All Rights Reserved.

processed in 0.204 (s). 11 q(s)