Skip to main content
K.Daube
Community Expert
Community Expert
June 15, 2016
Answered

Regular expression problem

  • June 15, 2016
  • 1 reply
  • 456 views

Dear friends,

In my script i have some sections which test the content of an edit field before it is processed further.

Things like the following work perfectly:

var re_Def = /#[A-Za-z][A-Za-z0-9_]+/;          // valid variable name ?
items = ["#correct", "notcorrect", "#This_is4", "#thisIs", "@something", "#ALLOK", "", ];
// search    0            -1          -1!!        -1!!          -1        -1!!     -1      <--- incorrect method
// test    true         false         true        true        false       true    false    <--- correct method
for (var j = 0; j < items.length; j++) {
  var item = items;
  alert ("'" + item + "' ==> " +  item.search(re_Def) + "\n" + re_Def.test(item));
}
var re_Def = /(\[ROW +\d+\]|\[COL +\d+\]|\[CELL +\d+, +\d+\]|Left *\(\d*\)|Right *\(\d*\)|Above *\(\d*\)|Below *\(\d*\))/;
items = ["[ROW 17]", "[Row n]", "[ROW n]", "[CELL 3, 9]", "[CELL 3 9]", "Abbove()", "Right(3)"];
// result  true        false      false         true         false        false         true   
for (var j = 0; j < items.length; j++) {
  alert ("'" + items + "' ==> " +  re_Def.test(items));
}

But the following always return false, independly of the content of the string item:

var re_Def = /{[EFJ]\d*}|{I}/;    // valid format def?
var item = "{E27}";
var result = re_Def.test(item);
alert (result);                   // false !!

RegEx Buddy tells me, that
- the REGEX is correct
- the result should be true, not false

- The verbose defintion of the RegEx is:
Match either the regular expression below (attempting the next alternative only if this one fails) «{[EFJ]\d*}»
   Match the character “{” literally «{»
   Match a single character present in the list “EFJ” «[EFJ]»
   Match a single digit 0..9 «\d*»
      Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
   Match the character “}” literally «}»
Or match regular expression number 2 below (the entire match attempt fails if this one fails to match) «{I}»
   Match the characters “{I}” literally «{I}»

Unrecognised typo? Faulty method test?

Results are wrong as soon as i use character list [...] - but look at the first code block: there are also character lists which are handled correctly.

This topic has been closed for replies.
Correct answer JoH

The curly brackets in the regular expression need to be escaped to be taken literally:

var re_Def = /\{[EFJ]\d*\}/;

Kind regards

JoH

1 reply

JoHCorrect answer
Inspiring
June 15, 2016

The curly brackets in the regular expression need to be escaped to be taken literally:

var re_Def = /\{[EFJ]\d*\}/;

Kind regards

JoH

K.Daube
Community Expert
K.DaubeCommunity ExpertAuthor
Community Expert
June 15, 2016

Thank You JoH,

Seems to be an error in RegexBuddy - as it accepts both escaped and unescaped braces here.