Bryan
Bryan

Reputation: 1

Javascript Expression - data extraction

I'm not very familiar with english and still less with javascript, sorry for that.

I have lots of extracted datas in a field named LT, I need to extract a particular data from LT field in javascript, this data is a number after "Relevé N° :" or sometimes after "N° de relevé :" expression is never exactly the same.

I actually use that: record.fields.LT.substring(record.fields.LT.indexOf("relev")).slice(10, 19);

But that dont match because sometimes there is more than one space or the "R" is uppercase etc..

Some help would be appreciated ;-)

Thanks Bryan

EDIT: thank you both for your answers, here is some screenshots for a better understanding 1

I try first suggestion and it ask for missing ; and ) but when i add it, nothing remain in field as you can see on picture2 2

and when i try second answer, there is a syntax problem(seems to be the "(?:")

Upvotes: 0

Views: 70

Answers (2)

Piyush
Piyush

Reputation: 1162

If the pattern you are looking for is only either 'Relevé N° :' or 'N° de relevé :' case insentitive, you can use a regex to capture the number that follows.

var testStrings = [
      "abc Relevé N° :12345678 string1",
      "123Relevé N° :1234567 string2",
      "aRelevé N° :1234567 string3",
      "N° de relevé :12345678 string4",
      "abc N° de relevé :12345678 string5"
    ];
    var myRegexp = /(Relevé N° :|N° de relevé :)([0-9]{7,8})/i;
    testStrings.forEach(function(str) {
      var match = myRegexp.exec(str)
      console.log(str + '   -> ' + match[2])
    })

Upvotes: 1

bharadhwaj
bharadhwaj

Reputation: 2139

I am not sure what you are asking for, but as per my understanding, the following regex should help.

 (?:((Relevé N° : )|(N° de relevé : )))[0-9]{7,8}

This uses a non-capturing group (not available in all regex implementations.) It will say, hey the String better have phrase like Relevé N° : or N° de relevé :, but I don't want this phrase to be part of my match, just the numbers (which is of length of 7 or 8) that follow.


Hope this helps! :)

Upvotes: 0

Related Questions