JavaScript Regular expressions in JavaScript: match multiple substrings at the beginning of a string? regular expressions javascript cheat sheet,regular expressions javascript tutorial,regular

I want to build a JavaScript regular expression to match all words starting with "ad" or "ae", or all words that contain "-ad" and "-ae".

This is what I've tried:

var regex_string = "^[ad|ae]|-[ad|ae]";
var re = RegExp(regex_string, "i");

var matches = _.filter(data, function(r) {
  if (re.test(r)) {
    return true;
  }
});

However, this is matching all words beginning with 'a', 'd' or 'e'.

How can I amend the regex to match only those strings?

JSFiddle here: http://jsfiddle.net/xGgan/1/

Answer:1

Because [] is match any letter inside So it says "match a or d or | or a or e". You need to use a capture group instead with the or.

Try

var regex_string = "(^(ad|ae)|-(ad|ae))";
Answer:2

[ ... ] denotes a character class and anything inside will match regardless of position. Additionally, [ae] matches only one character, either a or e.

For what your doing, translating it directly would give:

(?:^(?:ad|ae)|-(?:ad|ae))

You use | in groups ( ( ... ) for capture groups and (?: ... ) for non-capture groups; the latter are preferable if you don't intend to save captures for later use, as they improve the regex speed wise and memory wise).

But that can be optimised a bit:

(?:^|-)a[ed]

should match just as fine.

Answer:3

I have an controller which is simply watching a 'name' property and prints its new and old values on any change. function Ctrl($scope) { $scope.$watch('name', function(newValue, oldValue) { ...

I have an controller which is simply watching a 'name' property and prints its new and old values on any change. function Ctrl($scope) { $scope.$watch('name', function(newValue, oldValue) { ...

I am trying to replace all occurences of 'S' (capital S) with '.sss' : For example the following line doesn't even contain a capital S in it and it still replaces a lot : "MM/dd/yyyy EEEE hh:mm aa"....

I am trying to replace all occurences of 'S' (capital S) with '.sss' : For example the following line doesn't even contain a capital S in it and it still replaces a lot : "MM/dd/yyyy EEEE hh:mm aa"....

I am pushing HTML Code including <script> in a div, I am able to push inside the div successfully using innerHTML but script is not executing. Code: $.ajax({ data: '', dataType: 'html'...

I am pushing HTML Code including <script> in a div, I am able to push inside the div successfully using innerHTML but script is not executing. Code: $.ajax({ data: '', dataType: 'html'...

  1. delete script after execution
  2. script execution timed out after 30000ms
  3. remove script after execution
  4. exit shell script after execution
  5. pause powershell script after execution
  6. exit bash script after execution
  7. javascript remove script after execution
  8. powershell delete script after execution

Lets say I have some constructors: function Foo() { } function FooObject() { return {} } function FooArray() { return [] } function FooFunction() { return function () {} } function FooString() { ...

Lets say I have some constructors: function Foo() { } function FooObject() { return {} } function FooArray() { return [] } function FooFunction() { return function () {} } function FooString() { ...

  1. canvas
  2. canada
  3. cancelled
  4. canceled vs cancelled
  5. canada vaccines
  6. canada goose
  7. cane corso
  8. canes
  9. canadian prime minister
  10. cancun flights
  11. canker sore
  12. candy
  13. cancer
  14. cancun
  15. canon
  16. canvas prints
  17. cancer sign
  18. candace owens
  19. canada news
  20. candytopia