Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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字符串以获得所需的值_Javascript_String_Split - Fatal编程技术网

拆分javascript字符串以获得所需的值

拆分javascript字符串以获得所需的值,javascript,string,split,Javascript,String,Split,我想在此特定字符串中使用.split()从字符串中提取日期和用户名: var str ='<a href="/user/xxspmxx/profile">XxSPMxX</a> on 08/30/2012'; var str='2012年8月30日'; 我希望一个变量中包含XxSPMxX,另一个变量中包含08/30/2012 我认为split不是这个工作的合适工具。试试这个正则表达式: var str='2012年8月30日', name=str.match(/[^>

我想在此特定字符串中使用
.split()
从字符串中提取日期和用户名:

var str ='<a href="/user/xxspmxx/profile">XxSPMxX</a> on 08/30/2012';
var str='2012年8月30日';

我希望一个变量中包含
XxSPMxX
,另一个变量中包含
08/30/2012

我认为
split
不是这个工作的合适工具。试试这个正则表达式:

var str='2012年8月30日',

name=str.match(/[^>我认为
split
不是这个工作的合适工具。试试这个正则表达式:

var str='2012年8月30日',

name=str.match(/[^>仅使用
拆分

var x = str.split('</a> on ');

var name = x[0].split('>')[1];
var date = x[1];
var x=str.split('on'); var name=x[0]。拆分('>')[1]; var日期=x[1];

演示:

仅使用
拆分

var x = str.split('</a> on ');

var name = x[0].split('>')[1];
var date = x[1];
var x=str.split('on'); var name=x[0]。拆分('>')[1]; var日期=x[1];

演示:

另一种方法是使用正则表达式进行匹配,构建一个小数组以获取锚的部分,然后使用子字符串获取日期

var str = '<a href="/user/xxspmxx/profile">XxSPMxX</a> on 08/30/2012';
var matches = [];
str.replace(/[^<]*(<a href="([^"]+)">([^<]+)<\/a>)/g, function () {
    matches.push(Array.prototype.slice.call(arguments, 1, 4))
});
var anchorText = matches[0][2];
var theDate = str.substring(str.length - 10, str.length);

console.log(anchorText, theDate);
var str='2012年8月30日';
var匹配=[];

str.replace(/[^另一种方法是使用正则表达式进行匹配,构建一个小数组以获取锚的部分,然后使用子字符串获取日期

var str = '<a href="/user/xxspmxx/profile">XxSPMxX</a> on 08/30/2012';
var matches = [];
str.replace(/[^<]*(<a href="([^"]+)">([^<]+)<\/a>)/g, function () {
    matches.push(Array.prototype.slice.call(arguments, 1, 4))
});
var anchorText = matches[0][2];
var theDate = str.substring(str.length - 10, str.length);

console.log(anchorText, theDate);
var str='2012年8月30日';
var匹配=[];

str.replace(/[^Zoltan编辑问题之前没有。Zoltan编辑问题之前没有。