最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - JavaScript replace text between two special characters - Stack Overflow

programmeradmin1浏览0评论

I whould like to replace everything between the ;# characters with a new line (<br>). How can i do this in JavaScript?

Example:

String:

Beilagenteller - Mixed vegetables plate;#369;#Fischfilet mit Kräutersoße - Fish fillet, herbs sauce,;#183;#Rinderroulade "Hausfrauen Art", (S) Soße - Beef olive with sauce

Result:

Beilagenteller - Mixed vegetables plate
Fischfilet mit Kräutersoße - Fish fillet, herbs sauce,
Rinderroulade "Hausfrauen Art", (S) Soße - Beef olive with sauce

I whould like to replace everything between the ;# characters with a new line (<br>). How can i do this in JavaScript?

Example:

String:

Beilagenteller - Mixed vegetables plate;#369;#Fischfilet mit Kräutersoße - Fish fillet, herbs sauce,;#183;#Rinderroulade "Hausfrauen Art", (S) Soße - Beef olive with sauce

Result:

Beilagenteller - Mixed vegetables plate
Fischfilet mit Kräutersoße - Fish fillet, herbs sauce,
Rinderroulade "Hausfrauen Art", (S) Soße - Beef olive with sauce

Share Improve this question edited Aug 5, 2015 at 10:20 James Donnelly 129k35 gold badges215 silver badges223 bronze badges asked Aug 5, 2015 at 10:10 Nagy IstvanNagy Istvan 2023 silver badges13 bronze badges 1
  • Mhhmmm... Fischfilet mit Kräutersoße - such a delicious question :) – Octav Zlatior Commented Aug 5, 2015 at 10:22
Add a ment  | 

3 Answers 3

Reset to default 3

Assuming the value between the ;#s is always numeric, you can use the regular expression /;#\d*;#/g within a replace() call on the string:

var input = 'Beilagenteller - Mixed vegetables plate;#369;#Fischfilet mit Kräutersoße - Fish fillet, herbs sauce,;#183;#Rinderroulade "Hausfrauen Art", (S) Soße - Beef olive with sauce';

var output = input.replace(/;#\d*;#/g, "<br>");

document.write(output);

To show what the regular expression does, here is a visualisation:

You might want to try this:

var str = "Beilagenteller - Mixed vegetables plate;#369;#Fischfilet mit Kräutersoße - Fish fillet, herbs sauce,;#183;#Rinderroulade \"Hausfrauen Art\", (S) Soße - Beef olive with sauce";

str = str.replace(/;#[0-9]*;#/g, "<br/>");

console.log(str);

Use the folowing code

address="Beilagenteller - Mixed vegetables plate;#369;#Fischfilet mit Kräutersoße - Fish fillet, herbs sauce,;#183;#Rinderroulade 'Hausfrauen Art', (S) Soße - Beef olive with sauce"



address= str.replace(/;#[0-9]*;#/g, "\n");
发布评论

评论列表(0)

  1. 暂无评论