I have the following multi-line string displayed using <pre> like the following where each line is represented using new line:
SchoolDistrictCode:291238-9
location_version:3.4
build:e9kem
And I would like to parse build:e9kem out, and every time the content of the multi-line will vary. But it will have build: in every one.
I attempted it with:
const regexp = /^build:(.*)$/m;
//stringPar would vary every time
const stringPar = `SchoolDistrictCode:291238-9
location_version:3.4
build:e9kem`;
Attempt to display it like so:
{stringPar.match(regexp)[1])};
It parses correctly, but for certain multi-line string, it returns Uncaught TypeError: Cannot read property '1' of null when I try to display the string such as stringPar.match(regexp)[1], but it console logs the string properly when console logged like console.log(stringPar.match(regexp)[1]).
What could possibly be the issue?
Thank you in advance and will upvote/accept answer
EDIT (In ReactJS)
It's basically a component that's being rendered over and over, with the props being passed into it.
export default class tableRow extends Component {
...
render() {
console.log(this.props.stringPar.match(regexp)[1]);
return (
<tr>
<td>
{/*Even tried {this.props.stringPar.match(regexp)[1]==null? " ": this.props.stringPar.match(regexp)} but still getting the same error*/}
<button>{this.props.stringPar.match(regexp)[1]}</button>
<td>
<tr>
)
}
}
console.logthat fails in the same code block, and that illustrates the problem?Uncaught TypeError: Cannot read property '1' of nullthen it means that the return ofthis.props.stringPar.match(regexp)is returning null, not the value in the array, hence why the check you tried may not have worked. It looks like the regex is failing sometimes.{/*Even tried {this.props.stringPar.match(regexp)[1]==null? " ": this.props.stringPar.match(regexp)} but still getting the same error*/}but when I doconsole.log(stringPar.match(regexp)[1])it works though.