1

I'm building a script that tests if a variable is present in an object. To do that I use a regEx to test if that specific name is present in the object.

The problem I'm facing is that the JSON response is sometimes translated in German or Spanish. So in below case I want to test spec.title for the name Length (mm). This title also can be Länge (mm) and Longitud (mm).

In my project there's an already build in translation function called getAjaxTranslation('string') So this getAjaxTranslation('length') will return the translated string for length.

So my question is how can I use getAjaxTranslation('length') in below code to test?

So what I tried is:

reLength = '/'+ getAjaxTranslation('length') + '/i' //just to try
reLength = "'+ getAjaxTranslation('length') + '";
reLength = getAjaxTranslation('length')

Above tries give errors like reLength.test is not a function etc. So probably I'm working with wrong string/variables or doing something really wrong. I still have problems with those regExs...

Any help greatly appreciated!

Full code:

var data = {
  "product": {
    "specs": {
      "231638": {
        "id": 231638,
        "title": "Length (mm)",
        "value": "1200"
      },
      "231641": {
        "id": 231641,
        "title": "Width (mm)",
        "value": "800"
      },
      "231644": {
        "id": 231644,
        "title": "Height (mm)",
        "value": "144"
      } //etc etc
    }
  }
};

var length = 0, width = 0, height = 0,
  reLength = /length/i,
  reWidth = /width/i,
  reHeight = /height/i;
$.each(data.product.specs, function (specId, spec) {
  if (reLength.test(spec.title))
    length = spec.value;
  else if (reWidth.test(spec.title))
    width = spec.value;
  else if (reHeight.test(spec.title))
    height = spec.value;
});
1

1 Answer 1

4

Above tries give errors like reLength.test is not a function etc.

test is a method of RegExp object, make it

reLength =  new RegExp( getAjaxTranslation('length') , "i" )

Or use match instead of test, for example

reLength =  getAjaxTranslation('length'); //no need to make a regexp object
!!spec.title.match(reLength)

or

reLength =  getAjaxTranslation('length'); 
spec.title.includes(reLength) //use includes
Sign up to request clarification or add additional context in comments.

8 Comments

@RoryMcCrossan match will either return an array-link output or null, so match itself should help as well.
True, although using !! to coerce the type seems unnecessary when a simple test() call would work. Also note that I removed my downvote, however your first line - test is a method of RegExp function - is incorrect. test() is a method of the Regexp object, no matter if it's create explicitly (as in your example) or through literal notation (as in the OPs snippet). The important part of your solution is using the constructor with the concatenated string pattern.
@RoryMcCrossan Agree, I was trying to suggest that OP need not make a regex object and simply use the getAjaxTranslation('length'). I will update the answer accordingly
@RoryMcCrossan I corrected this test is a method of RegExp object as well. This is what I had meant as well earlier.
@gurvinder372: Ok interesting. Will match also work when some spec.titles are almost the same? So let's say these two spec titles: length and length (mm) for example? Or is match used to exactly match whole string?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.