热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

javascript获取两组字符之间的子字符串-javascriptgetsubstringbetweentwosetsofcharacters

Ihavethefollowingstring:我有以下字符串:varmyString<p><i><i>

I have the following string:

我有以下字符串:

var myString = '

';

how can i get with the least code the someExtraStuff.fileExt section?

我如何用最少的代码获得someExtraStuff.fileExt部分?

should i do indexOf {{appName}} and then until the next "/> ?

我应该做indexOf {{appName}}然后直到下一个“/>?

2 个解决方案

#1


2  

You could search for the pattern {{appName}} and take all characters who are not quotes. Then take the second element of the match.

您可以搜索模式{{appName}}并获取所有不是引号的字符。然后拿下比赛的第二个元素。

var string = '

', substring = (string.match(/\{\{appName\}\}([^"]+)/) || [])[1] console.log(substring);

#2


2  

You can do this with three methods

您可以使用三种方法完成此操作

// 1 option For single match

// 1选项用于单场比赛

var regex = /\{\{appName\}\}([^"]+)/;
var myString = '

'; console.log(myString.match(regex)[1]);

// 2 option For multiple matches

// 2选项用于多个匹配

var regex = /\{\{appName\}\}([^"]+)/g;
var myString = '

'; var temp; var resultArray = []; while ((temp = regex.exec(myString)) != null) { resultArray.push(temp[1]); } console.log(resultArray);

// 3 option For indexOf

// 3选项对于indexOf

var firstIndex= myString.indexOf("{{appName}}");
var lastIndex =firstIndex+ myString.substring(firstIndex).indexOf('"/>')
var finalString = myString.substring(firstIndex,lastIndex).replace("{{appName}}","");
console.log(finalString);

推荐阅读
author-avatar
手机用户2602921931
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有