Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript+;ReactJS:为什么';t regex.match()是否不一致地返回?_Javascript_Html_Regex_Reactjs_React Jsx - Fatal编程技术网

Javascript+;ReactJS:为什么';t regex.match()是否不一致地返回?

Javascript+;ReactJS:为什么';t regex.match()是否不一致地返回?,javascript,html,regex,reactjs,react-jsx,Javascript,Html,Regex,Reactjs,React Jsx,我使用 SchoolDistrictCode:291238-9 location_version:3.4 build:e9kem 我想解析build:e9kemout,每次多行的内容都会有所不同。但它在每一个版本中都有build: 我尝试过: {stringPar.match(regexp)[1])}; 尝试这样显示: export default class tableRow extends Component { ... render() { c

我使用

SchoolDistrictCode:291238-9
location_version:3.4
build:e9kem
我想解析
build:e9kem
out,每次多行的内容都会有所不同。但它在每一个版本中都有
build:

我尝试过:

{stringPar.match(regexp)[1])};
尝试这样显示:

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>
          )
     }
}
它解析正确,但对于某些多行字符串,当我尝试显示字符串(如
stringPar.match(regexp)[1]
)时,它返回
uncaughttypeerror:cannotread属性“1”为null,但当控制台像console.log(stringPar.match(regexp)[1]一样记录字符串时,它会正确记录字符串

可能是什么问题

提前感谢您,并将投票/接受答案

编辑(在ReactJS中)

它基本上是一个被反复渲染的组件,道具被传递到其中

export default class tableRow extends Component {
     ...

     render() {
          // ensure we actually have a string to run match on so don't get an error
          const matchStr = this.props.stringPar || ''; 
          const m = matchStr.match(regexp); // run match
          const matchFound = m && m.length > 0; // boolean, was match found
          const build = matchFound ? m[1] : 'build fallback'; // regex match or fallback if no match was found

          if (!matchFound) {
            console.log("Could not find 'build:' in:", matchStr, matchStr.length === 0 ? 'BLANK!' : 'NOT BLANK');               
          }

          return (
               <tr>
                 <td>
                   <button>{build}</button>
                 <td>
               <tr>
          )
     }
}
导出默认类tableRow扩展组件{
...
render(){
log(this.props.stringPar.match(regexp)[1]);
返回(
{/*甚至尝试了{this.props.stringPar.match(regexp)[1]==null?”:this.props.stringPar.match(regexp)},但仍然得到相同的错误*/}
{this.props.stringPar.match(regexp)[1]}
)
}
}
可能是什么问题

正则表达式在所有情况下都不匹配,可能是因为:

每次多行的内容都会有所不同

执行类似操作,以便可以看到字符串不匹配时的外观:

导出默认类tableRow扩展组件{
...
render(){
//确保我们确实有一个字符串来运行匹配,这样就不会出现错误
const matchStr=this.props.stringPar | | |“”;
const m=matchStr.match(regexp);//运行match
const matchFound=m&&m.length>0;//布尔值,已找到匹配项
const build=matchFound?m[1]:“build fallback”;//如果找不到匹配项,则为regex match或fallback
如果(!找到匹配项){
log(“在:”,matchStr,matchStr.length==0?“BLANK!”:“notblank!”中找不到“build:”;
}
返回(
{build}
)
}
}
并相应地调整您的正则表达式

可能是什么问题

正则表达式在所有情况下都不匹配,可能是因为:

每次多行的内容都会有所不同

执行类似操作,以便可以看到字符串不匹配时的外观:

导出默认类tableRow扩展组件{
...
render(){
//确保我们确实有一个字符串来运行匹配,这样就不会出现错误
const matchStr=this.props.stringPar | | |“”;
const m=matchStr.match(regexp);//运行match
const matchFound=m&&m.length>0;//布尔值,已找到匹配项
const build=matchFound?m[1]:“build fallback”;//如果找不到匹配项,则为regex match或fallback
如果(!找到匹配项){
log(“在:”,matchStr,matchStr.length==0?“BLANK!”:“notblank!”中找不到“build:”;
}
返回(
{build}
)
}
}

并相应地调整正则表达式。

“对于某些多行字符串”:请给出一个具体的例子。您的问题也可能与异步代码有关。您能否提供一个代码段,该代码段的console.log在同一代码块中失败,并说明了问题?@trincot请查看编辑如果您得到的错误是
Uncaught TypeError:Cannot read property'1'of null
,则表示返回
this.props.stringPar.match(regexp)
返回的是null,而不是数组中的值,因此您尝试的检查可能不起作用。看起来正则表达式有时会失败。@jonowzz,但我尝试了
{/*甚至尝试了{this.props.stringPar.match(regexp)[1]==null?”:this.props.stringPar.match(regexp)},但仍然得到相同的错误*/}
,但当我尝试
控制台.log(stringPar.match(regexp)[1])
但它是有效的。在你的问题中没有具体的失败例子。请提供一个我们可以用来重现问题的片段。可能在JSFIDLE中,“对于某些多行字符串”:请给出一个具体的例子。您的问题也可能与异步代码有关。您能否提供一个代码段,该代码段的console.log在同一代码块中失败,并说明了问题?@trincot请查看编辑如果您得到的错误是
Uncaught TypeError:Cannot read property'1'of null
,则表示返回
this.props.stringPar.match(regexp)
返回的是null,而不是数组中的值,因此您尝试的检查可能不起作用。看起来正则表达式有时会失败。@jonowzz,但我尝试了
{/*甚至尝试了{this.props.stringPar.match(regexp)[1]==null?”:this.props.stringPar.match(regexp)},但仍然得到相同的错误*/}
,但当我尝试
控制台.log(stringPar.match(regexp)[1])
但它是有效的。在你的问题中没有具体的失败例子。请提供一个我们可以用来重现问题的片段。可能在jsfiddle中。请欣赏响应,但对于某些组件,相同的错误将在
this.props.stringPar.match(regexp)处停止程序。我应该在那里做什么样的检查?@JoKo听起来好像有时候道具没有设置好。我已经更新了我的答案。@JoKo不确定我是否明白。有时是
build
,有时是
dailyBuild
export default class tableRow extends Component {
     ...

     render() {
          // ensure we actually have a string to run match on so don't get an error
          const matchStr = this.props.stringPar || ''; 
          const m = matchStr.match(regexp); // run match
          const matchFound = m && m.length > 0; // boolean, was match found
          const build = matchFound ? m[1] : 'build fallback'; // regex match or fallback if no match was found

          if (!matchFound) {
            console.log("Could not find 'build:' in:", matchStr, matchStr.length === 0 ? 'BLANK!' : 'NOT BLANK');               
          }

          return (
               <tr>
                 <td>
                   <button>{build}</button>
                 <td>
               <tr>
          )
     }
}