JavaScript Wildcards (.*) with two optional (?) regex parts

I want to have two optional parts in my regex - either of them can be found in matching string, and also both of them can at the same time.

Both of those optional parts consist of a wildcard (I wrote .* here but it can be say [a-zA-Z]+; anyway the point is that second part of the regex (Bar) matches the wildcard if greedy matching is applied).

It's best illustrated by this Firebug output:

>>> /(Foo.*)?(Bar.*)?/.exec("FooAA")
["FooAA", "FooAA", undefined] // OK

>>> /(Foo.*)?(Bar.*)?/.exec("BarAA")
["BarAA", undefined, "BarAA"] // OK

>>> /(Foo.*)?(Bar.*)?/.exec("FooAABarAA")
["FooAABarAA", "FooAABarAA", undefined] // I'd like to have ["FooAABarAA", "FooAA", "BarAA"]

Is there a way to construct a regex matching my three test cases above?

I can use sth like

/((Foo.*)(Bar.*))|(Foo.*)?(Bar.*)?/.exec("FooAABarAA")

but I want to avoid duplication.

Answer:1

You need non-greedy wildcards. This works for me:

/^(Foo.*?)?(Bar.*?)?$/
Answer:2

You should simply be able to make the first wildcard non-greedy like this:

/(Foo.*?)?(Bar.*)?/
Answer:3

In the parent html page , when I click a link fancybox dialog opens up and I load a laravel php view page in that fancybox. When I press 'login with twitter' button in fancybox dialog, then I want ...

In the parent html page , when I click a link fancybox dialog opens up and I load a laravel php view page in that fancybox. When I press 'login with twitter' button in fancybox dialog, then I want ...

In java, I have this URL as a string: window.location.href = "http://localhost:8080/bladdey/shop/c6c8262a-bfd0-4ea3-aa6e-d466a28f875/hired-3"; I want to create a javascript regular expression to ...

In java, I have this URL as a string: window.location.href = "http://localhost:8080/bladdey/shop/c6c8262a-bfd0-4ea3-aa6e-d466a28f875/hired-3"; I want to create a javascript regular expression to ...

  1. javascript string pull
  2. js retrieve string
  3. javascript pull query string
  4. javascript pull out string
  5. javascript string retrieve
  6. javascript pull number from string
  7. javascript pull date from string
  8. javascript pull number out of string

I'm running the following code to set a limit on characters in a textarea: (function($){ $.fn.extend({ limit: function(limit,element) { var interval, f; var self = $(this); ...

I'm running the following code to set a limit on characters in a textarea: (function($){ $.fn.extend({ limit: function(limit,element) { var interval, f; var self = $(this); ...

  1. count carriage return javascript
  2. count carriage return excel
  3. count carriage return c#
  4. count carriage return java
  5. count carriage return python
  6. excel count carriage returns in cell
  7. javascript count carriage returns in string
  8. vba count carriage returns in string
  9. c# count carriage returns in a string
  10. sql count carriage returns
  11. jquery count carriage return textarea
  12. count lines carriage return
  13. count number of carriage returns excel
  14. vb.net count carriage returns in string
  15. count number of carriage returns vba
  16. php count carriage return
  17. js count carriage returns

I know how to click a link using javascript, document.getElementById('yourLinkID').click(); But what if your ID is always changing or you are not sure what your ID is going to be? I have a html ...

I know how to click a link using javascript, document.getElementById('yourLinkID').click(); But what if your ID is always changing or you are not sure what your ID is going to be? I have a html ...

  1. click save html