'JavaScript If statement ERROR - null is not an object (evaluating
I am using TagUI(js based automation tool) and javascript, and while writing code to extract information from the a string I receive a "null is not an object (evaluating 'd1.match(/(Internet provider)(.*)(<br/>)/ig)[0];'
Where d1 can contain a couple of fields that I want to extract as parameters if a certain field is not available I would like to replace it with "not applicable"
examples:
d1 = "<p>please create a new entry.</p> <p>Person's name: Tom Jones<br/> Company: some company <br/> address: street one <br/> Employee ID: 07092120 <br/></p> "
OR
d1 = "<p>please create a new entry.</p> <p>Person's name: Jon Tomes<br/> Company: some company2 <br/> address: street two <br/><br/> Internet provider: O2 <br/> Employee ID: 07092121 <br/></p>"
I have tried the following expressions with no luck
provider = d1.match(/(Internet provider)(.*)(<br\/>)/ig)[0];
if (typeof ((d1.match(/(Internet provider)(.*)(<br\/>)/ig)[0]) === 'object' && (d1.match(/(Internet provider)(.*)(<br\/>)/ig)[0]) !== null)){provider=(d1.match(/(Internet provider)(.*)(<br\/>)/ig)[0]);}else{provider="not applicable"; }
if (d1.match(/(Internet provider)(.*)(<br\/>)/ig)[0] !== null){provider="not applicable";}else{provider = d1.match(/(Internet provider)(.*)(<br\/>)/ig)[0];}
if (typeof (provider = d1.match(/(Internet provider)(.*)(<br\/>)/ig)[0]) !== 'string'){provider="not applicable";}else{provider=d1.match(/(Internet provider)(.*)(<br\/>)/ig)[0]; }
Solution 1:[1]
@VLAZ's comment did the trick:
If
.match()doesn't match anything, the result is null..match()[0]would throw an error in that case.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tomerikoo |
