我有一个(AngularJS)项目,我将替换文本中的一些标签.举例:
{tag1} => one
{tag2} => two
{tag3} => three
Run Code Online (Sandbox Code Playgroud)
使用JavaScript替换功能可以替换一个值:
text.replace('{tag1}', 'one');
Run Code Online (Sandbox Code Playgroud)
但我有25个标签的列表.我想用标签和替换物制作一个对象.举例:
var tags = {"{tag1}":"one", "{tag2}":"two", "{tag3}":"three"};
text.replace(tags);
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
当我将"123"值输入到输入字段时它将正确转换为字母"ABC",但是当我输入"112"时它只转换第一个数字,如"A1B"我需要"112"转换为"AAB".但是,重复的字符不会被替换.
function char_convert() {
var chars = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"];
var codes = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"];
for (x = 0; x < chars.length; x++) {
for (i = 0; i < arguments.length; i++) {
arguments[i].value = arguments[i].value.replace(chars[x], codes[x]);
}
}
}
char_convert(this);Run Code Online (Sandbox Code Playgroud)
<div id="test">
<input type="text" id="txtBox" onchange="char_convert(this);" />
</div>Run Code Online (Sandbox Code Playgroud)
我正在使用javascript函数,在给定的字符串中我需要替换现在//只有一个斜杠/我有:
result= mystring.replace("\/\/", "/");
Run Code Online (Sandbox Code Playgroud)
它没有工作,我仍然得到双斜杠字符串,所以这是正确的正则表达式,以指示替换函数的双斜杠?
我已经尝试过:
编辑:
我正在使用它来更正保存在字符串中的URL,例如,有时该URL可能类似于:mywebpage/someparameter//someotherparameter并且双斜杠会产生问题,因此我需要将其替换为单个斜杠,如:mywebpage/someparameter/someotherparameter
我正在尝试删除字符串中的空格,但它似乎不起作用。这不会返回不带空格的字符串:
"{ color: 'blue',".replace(" ",'')和"{ color: 'blue',".trim()
我已经验证这些是空格(字符代码 32),我什至已经这样做了:
var x = "{ color: 'blue',"
x.replace(x[1],'')
仅供参考,在 Chrome 的控制台中执行此操作。我要疯了吗?
您好,我有一个车牌号BZ8345LK,想要转换为BZ 8345 LK(在char和number之间添加空格)。
我尝试使用此Regex,但无法正常工作,只能将带数字的第一个字符设为空格。例如BZ 8345LK,' LK'不能与数字保持空格。
var str = 'BZ8345LK';
str.replace(/[^0-9](?=[0-9])/g, '$& ');
# return BZ 8345LK, I want BZ 8345 LK
Run Code Online (Sandbox Code Playgroud) 我得到了上传图片的生成路径:
'..\assets\dist\upload\avatar.jpg'
Run Code Online (Sandbox Code Playgroud)
我想用这种格式。
'../assets/dist/upload/avatar.jpg'
Run Code Online (Sandbox Code Playgroud)
我尝试过 .replace(/[\]/g, '/');
但是没有和我一起工作。
另外,我咨询了该线程,但没有解决我的问题。没有将反斜杠转换为正斜杠的示例。
如果我们在谈论,这是可能的'..\\assets\\dist\\upload\\avatar.jpg'。但是,我的情况是'..\assets\dist\upload\avatar.jpg'。
您对解决这个问题有任何想法吗?
非常感谢。
我有一个字符串,其中包含单词,后跟冒号。我需要用对象中的值替换该字符串中的冒号。我能够提取出冒号单词,但不确定在字符串中替换它的最佳方法。
这就是我所拥有的:
const string = 'This is :state :buttonName by :name';
const buttonName = 'button link';
const data = {
state: 'Alabama',
name: 'Arun'
}
const res = string.match(/:[a-zA-Z]+/g).map(i => i.replace(':', ''))
console.log(res)
// This is Alabama button link by ArunRun Code Online (Sandbox Code Playgroud)
最终结果应该是
This is Alabama button link by Arun
Run Code Online (Sandbox Code Playgroud)
请指教。
我用它来插入一些字符串数据:
$("#edit_order #"+key).val(value.replace('+',' '));
Run Code Online (Sandbox Code Playgroud)
但是,“+”的第二个实例不会被此字符串替换:
123123123+APT+123
Run Code Online (Sandbox Code Playgroud)
相反,我得到输出:
123123123 APT+123
Run Code Online (Sandbox Code Playgroud) 我有一个字符串以这种方式格式化到我的WebApp:
GPL.TU01<50;0;100;0;0>
我不得不这样说:
GPL.TU01
<
50;
0;
100;
0;
0
>
Run Code Online (Sandbox Code Playgroud)
这就是我正在使用的:
var GET_result_formatted = GET_result;
global_file_content = GET_result;
GET_result_formatted = GET_result_formatted.replace("<", "\r<\r");
GET_result_formatted = GET_result_formatted.replace(';', ";\r");
GET_result_formatted = GET_result_formatted.replace(">", "\r>");
$('#ModalGPLTextarea').val(GET_result_formatted);
Run Code Online (Sandbox Code Playgroud)
但令人遗憾的结果是:
GPL.TU01
<
50;
0;100;0;0
>
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
考虑以下代码:
var text2='ain';
let text = "The rain in SPAIN stays mainly in the plain";
let result = text.replaceAll(/text2/g,'__');
document.getElementById("demo").innerHTML = result;Run Code Online (Sandbox Code Playgroud)
<p id="demo"></p>Run Code Online (Sandbox Code Playgroud)
字符串result与 相同text。我该如何解决这个问题?我只想将ain文本字符串中的每个内容替换为___.