簡體   English   中英

替換為\\ n <br> 在JSON字符串中

[英]Replace \n with <br> in JSON string

我有一個函數來抓取文本數據作為JSON,然后進行字符串化。 我正在嘗試在我的Web應用程序中以模式顯示此數據,但字符串在HTML中顯示如下:

{“body”:“--- \\ nbl_playout_user:BLplayout \\ n \\ n#播放配置值\\ nbl_playout_image_drive_mount:'\\\\ Paparazzi \\ Images'\\ nbl_playout_image_path:'我:'\\ nbl_playout_machine_role:Primary \\ nbl_playout_network_name:ABC1 \\ nbl_playout_stand_alone_mode:0 \\ nbl_playout_run_mode:HD \\ nbl_playout_format:AJA \\ nbl_playout_resolution:720P \\ nbl_playout_logo_override:ABC \\ nbl_playout_default_header:底線\\ nbl_playout_sponsor_on:15000 \\ nbl_playout_sponsor_off:60000 \\ nbl_playout_media_ball_on:15000 \\ nbl_playout_media_ball_off:15000 \\ nbl_playout_page_engine_working_directory: '{{bl_templates_root_dir}} \\ SC14_Templates' \\ nbl_playout_schema_file_path : '{{bl_templates_root_dir}} \\ SC14_Templates \\ BLMaster.xsd' \\ nbl_playout_default_swatch_path: '{{bl_templates_root_dir}} \\ SC14_Templates \\ 003_BtmLn_deliverable_ele \\ still_ele \\ RDBL_GENERIC_SWATCH.png' \\ nbl_playout_default_logo_path:“{{bl_templates_root_dir}} \\ SC14_Templates \\ 003_BtmLn_deliverable_ele \\ still_ele \\ default_team_logo.png'\\ n“}

我希望將\\n的實例替換為<br>以便它在HTML中正確顯示,但是我的功能似乎沒有做到這一點。 任何想法為什么? 我很難過。

var stringData = JSON.stringify(data);
stringData = stringData.replace(new RegExp('\r?\n','g'), '<br />');
return stringData;

嘗試這個。

stringData = stringData.replace(new RegExp("\\\\n", "g"), "<br />");

或這個

stringData = stringData.replace(/\\n/g, "<br />");

基於評論和建立其他答案..

var json = JSON.parse(stringData);
json.body = body = json.body.replace(/\n/g, "<br />");

// and then if you needed it in a string you can do this again
stringData = JSON.stringify(json);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM