JavaScript来搜索字符串和大括号中的所有大写标记
JavaScript to search a string and brace all capitalzied tokens
我需要JavaScript在字符串中搜索所有大写的令牌,不包括第一个,并用括号中的令牌替换所有这些令牌。
例如
"Rose Harbor in Bloom: A Novel" -> "Rose {Harbor} in {Bloom}: {A} {Novel}"
"In America: a novel" -> "In {America}: a novel"
"What else. In the country" -> "What else. {In} the country"
您可以使用$&在替换中使用匹配的字符串
此外,'b
指定单词边界,[A-Z]
将指定所有大写字符*?
尝试使用尽可能少的字符进行匹配
所以你想要.replace(/'b[A-Z].*?'b/g, '{$&}')
举个例子:
"a String is Good yes?".replace(/'b[A-Z].*?'b/g, '{$&}')
返回
"a {String} is {Good} yes?"
对于exclude the first token
,你必须有一点创造性;
function surroundCaps(str) {
//get first word
var temp = str.match(/^.+?'b/);
//if word was found
if (temp) {
temp = temp[0];
//remove it from the string
str = str.substring(temp.length);
}
else
temp = '';
str = str.replace(/'b[A-Z].*?'b/g, '{$&}');
return temp + str;
}
surroundCaps('String is Good Yes'); //'String is {Good} {Yes}'
相关文章:
- 使用javascript或angularjs特定过滤器搜索字符串中第一个img标记的json值
- 在javascript中搜索字符串
- td:包含变量中的搜索字符串
- 在元素中搜索字符串,然后将其放入条件语句中
- Javascript正则表达式,用于验证接收到的MySQL全文搜索的搜索字符串
- 在对象数组中搜索字符串
- 在HTML中搜索字符串并返回True的Javascript条件
- 使用Tilde(~)和search()搜索字符串
- 在DOM中搜索字符串时不区分大小写
- 如何以编程方式输入搜索字符串并触发谷歌地图API的places_changed
- 在 Enter 上,在 db 中搜索字符串
- 使用引导表插件,如何在首次加载时设置搜索字符串
- 如何在 Firefox 25 中获取查找栏的最后一个搜索字符串
- JavaScript 搜索字符串包含
- 如何确定搜索字符串是否被较大字符串中的锚标记包装 - javascript 正则表达式
- 使用 forEach 在多个数组中搜索字符串
- 如何编写 angularjs 过滤器来搜索字符串片段/字符串序列
- Javascript:搜索字符串时数组中的多个表达式
- 如何使用 javascript 在
中搜索字符串,然后添加到该 id - 谷歌表格循环搜索字符串并清除相邻单元格