Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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 将字符串html转换为数组对象类型_Javascript_Regex_React Native - Fatal编程技术网

Javascript 将字符串html转换为数组对象类型

Javascript 将字符串html转换为数组对象类型,javascript,regex,react-native,Javascript,Regex,React Native,我有字符串内容html示例: Yjgbhg-Huyju 我想把字符串传递给同一个对象 [ { type: "text", value: "Yjgbhg" }, { type: "img", value: "https://dzlvqfm687ile.cloudfront.net/5940_image-0878eae8-e316-46fc-a776-b3d22e292c55.jpg" }, { type: "text", valu

我有字符串内容html示例:

Yjgbhg-Huyju

我想把字符串传递给同一个对象

[
  {
    type: "text",
    value: "Yjgbhg"
  },
  {
    type: "img",
    value: "https://dzlvqfm687ile.cloudfront.net/5940_image-0878eae8-e316-46fc-a776-b3d22e292c55.jpg"
  },
  {
    type: "text",
    value: "Huyju"
  },
  {
    type: "text",
    value: "https://dzlvqfm687ile.cloudfront.net/5824_video.mp4"
  },

]
我试着用

var regexp=//g;
log(regexp.exec(strHtml));
while(m=regexp.exec(text)){
push(m[0]);
}
var regexp=/]*src=“?([^”\s]+)”?\s*\/>/img;
while(m=regexp.exec(text)){
push(m[0]);
}

console.log(URL);
使用DOMParser可能会容易得多:

const input=`Yjgbhg Huyju`;
const parser=new DOMParser().parseFromString(输入'text/html');
const text1=parser.body.childNodes[0].textContent.trim();
const img=parser.body.children[0].src;
const text2=parser.body.childNodes[2].textContent.trim();
const video=parser.body.children[1].src;

log([text1,img,text2,video]);
您可以将其解析为DOM树,然后将DOM树的节点映射到对象

在下面的代码中,假设您只有要检索
src
属性的文本节点和元素节点。如果还有其他节点,则必须向映射添加一些逻辑

const htmlToObject = (html) => {
  const dom = new DOMParser().parseFromString(html, 'text/html');
  const object = Array.from(dom.body.childNodes).reduce((a, v) => {
    switch(v.nodeType) {
      case 1: // element node
        a.push({type: v.tagName.toLowerCase(), value: v.src});
        break;
      case 3: // text node
        a.push({type: 'text', value: v.textContent.trim()});
        break;
    }

    return a;
  }, []);

  return object;
}
完整片段:

consthtmltobject=(html)=>{
const dom=new DOMParser().parseFromString(html,'text/html');
const object=Array.from(dom.body.childNodes).reduce((a,v)=>{
开关(v.nodeType){
案例1:
a、 push({type:v.tagName.toLowerCase(),value:v.src});
打破
案例3:
a、 push({type:'text',value:v.textContent.trim()});
打破
}
返回a;
}, []);
返回对象;
}
const html='Yjgbhg Huyju';

log(htmlToObject(html));
我尝试导入{DOMParser}从'react native'可以看出,
\u reactNative.DOMParser不是构造函数
它是
窗口的属性
,不是
reactNative的属性
,正如您在代码段中看到的,如果您的代码运行,它将为true,那么它将只会是字符串,不处理其他情况什么其他情况?您的问题只是说
我有字符串
对不起,我把我的问题编辑成了示例字符串,我的badIt show
DOMParser没有定义
如何导入它?对不起,我是android开发者show,我不知道更多的JS和RNIt show
DOMParser没有定义
。这是什么?这是我的代码。你能检查一下吗?所以你只需要添加一个DOMParser,例如使用我安装的DOMParser和它正在工作,但是当我
console.log(dom.body.childNodes)
它返回
undefined
,它是
dom.childNodes
console.log(Array.from(parsed.childNodes))时
它显示数组3项,但在数组中看不到src,我可以看到namespaceURI,但节点的值是
http://www.w3.org/1999/xhtml
标签视频和img