/    Sign up×
Community /Pin to ProfileBookmark

How to extract text from string according to mask in JavaScript?

I am working in a license plate reading app using JavaScript. When I read the plate some additional text tends to be included as below: “280SILRJY2G74BR” I know that the license plate mask is XXXNXNN (X for letters and N for numbers) The number of characters and placement that are added varies with several conditions like image quality, proximity of the camera, state of conservation of the plate, etc So, in the example above, if I apply the given mask the real plate number, RJY2G74 is the part that would satisfy the mask.

After researching the subject and some help I arrived to:
RegExp(/[A-Z]{3}d{1}[A-Z]{1}d{2}/.exec(myText)?.[0]);
But it returns: “/(?:)/” and I cannot figure out why.

Assistance welcome.

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumSep 13.2021 — Unfortunately a part of your search result was converted to a smilie. Therefore I enclosed it by single backticks.
Copy linkTweet thisAlerts:
@SempervivumSep 13.2021 — Whats going on here is the following: This expression:

`/[A-Z]{3}d{1}[A-Z]{1}d{2}/.exec(myText)?.[0]</C><br/>
is evaluated and the result is handed over to <C>
RegExp</C>. The result is a RegExp object.<br/>
However you intend to extract the license plate which is a string. This can be done easier and more straight forward by this code:<br/>
<C>
const licensePlate = /[A-Z]{3}d{1}[A-Z]{1}d{2}/.exec(myText);</C><br/>
Unfortunately I do not understand the trailing part of your syntax: <C>
?.[0]`


Maybe one of our experts for tricky coding can explain ;)
Copy linkTweet thisAlerts:
@pcborgesauthorSep 13.2021 — @Sempervivum#1636912 Hi, works great...

"I do not understand the trailing part of your syntax: ?.[0]"

Neither do I, I got this somewhere but did not work

Thanks a lot

Paulo
Copy linkTweet thisAlerts:
@NogDogSep 13.2021 — > @Sempervivum#1636912 I do not understand the trailing part of your syntax: ?.[0]

I'm no JS expert, but looks like RegExp returns an array of matches, so I'm guessing that's just saying "take the first match"?
Copy linkTweet thisAlerts:
@SempervivumSep 13.2021 — @NogDog#1636939 Yes, this would explain the `[0]</C> but I have no clue what <C>?.</C> means. A ternary expression would require a <C>:`
Copy linkTweet thisAlerts:
@NogDogSep 13.2021 — Ah, we can both learn something new today: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining

(At first I thought maybe it was like the convention in Ruby of appending a ? to the name of any method that returns Boolean, but apparently ?. is an actual operator in JS.)
Copy linkTweet thisAlerts:
@SempervivumSep 13.2021 — I see, that's a cool feature, thanks for this hint.
×

Success!

Help @pcborges spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...