0

I'm trying to do something very simple, but I can't get to work the way I intend. I'm sure it's doing exactly what I'm asking it to do, but I'm failing to understand the syntax.

Part 1:

In the following example, I want to extract the part of the string between geotech and Input.

x = "geotechCITYInput" x.match(/^geotech(.*)(?:Input|List)$/) 

The result:

["geotechCITYInput", "CITY"] 

I've been writing regex for many years in perl/python and even javascript, but I've never seen the ?: syntax, which, I think, is what I'm supposed to use here.

Part 2:

The higher level problem I'm trying to solve is more complicated. I have a form with many elements defined as either geotechXXXXInput or geotechXXXXList. I want to create an array of XXXX values, but only if the name ends with Input.

Example form definition:

obj0.name = "geotechCITYInput" obj1.name = "geotechCITYList" obj2.name = "geotechSTATEInput" obj3.name = "geotechSTATEList" 

I ultimately want an array like this:

["CITY","STATE"] 

I can iterate over the form objects easily with an API call, but I can't figure out how to write the regex to match the ones I want. This is what I have right now, but it doesn't work.

geotechForm.forEachItem(function(name) { if(name.match(/Input$/) inputFieldNames.push( name.match(/^geotech(.*)Input$/) ); }); 

Any suggestions would be greatly appreciated.

7
  • With x.match(/^(?:geotech)(.*)$/) how are you expecting to get the text between geotech and Input when you aren't even including Input in the regex? Commented Jul 17, 2014 at 16:30
  • I'm not sure what I'm doing. Literally any suggestion is welcome even if it means using a totally different approach. Commented Jul 17, 2014 at 16:31
  • 1
    x.match(/^geotech(.*)Input$/) -> ["geotechCITYInput", "CITY"] Commented Jul 17, 2014 at 16:31
  • @Anthony you said you've been writing regex for many years. This has nothing to do with the syntax. It's pretty clear you aren't going to get the text you want with what you were trying. Commented Jul 17, 2014 at 16:33
  • 1
    match returns an array and you want element [1] Commented Jul 17, 2014 at 16:45

1 Answer 1

1

You were missing the Input and List suffix in your regex. This will match if the name starts with geotech and ends with either Input or List and it will return an array with the text in the middle as the second item in the array.

geotechForm.forEachItem(function (name) { var match = name.match(/^geotech(.*)(Input|List)$/); if (match) { inputFieldNames.push(match[1]); } }); 
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your help so far. I see you're using an index, which makes sense to me. At this point, I suppose I'm asking a more academic question. How do you limit the regex to not match the substring 'geotech' in any case? I know the concept of a backreference exists in Javascript, but nothing I've tried has worked at this point.
/^geotech(.*)Input|List$/ not /^geotech(.*)(Input|List)$/
@Anthony my answer is a working solution. If you want to spend time figuring out how to only return the one string in the array, go for it. It really makes no difference.
Of course It doesn't :) The output is ["geotechSTATEInput", "STATE", "Input"] and you don't need the Input part. Read the question again. Even List part isn't needed ,according to the 'Part 2' or the question.
@hex494D49 if you would look at my solution, I am extracting "STATE" from the array, so it doesn't matter that the array contains more items than needed.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.