简体   繁体   中英

How can I extract two strings from a string with javascript?

I have a string that contains the following:

<form class="form" 
data-action="Create" 
data-entity="Topic" 
data-href="/Admin/Contents/JsonCreate" 
data-rowkey="007H" 
data-partitionkey="0006000" id="form">

How can I get the values of data-rowkey and data-partitionkey from this string and put into javascript variables?

Please note my data is not part of a page. This is still data in a string.

let formstring是包含表单声明的字符串:

formstring.match(/data-rowkey=.+?"/i)[0].split('=')[1].replace(/"/g,'');

如果你使用jquery,你总是可以去

var data = $('.form').attr('data-rowkey);
$("#form").attr("data-rowkey") 
var str = '<form class="form" data-action="Create" 
data-entity="Topic" data-href="/Admin/Contents/JsonCreate" 
data-rowkey="007H" data-partitionkey="0006000" id="form">';

var rowkey = str.match(/data-rowkey=(.+) data-partitionkey=(.+)/)[1];
var partition =str.match(/data-rowkey=(.+) data-partitionkey=(.+) id="form"/)[2];

FIDDLE DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM