
[英]How to remove double quotes from default string in javascript/Jquery
[英]How to remove double quotes from JS string?
我的字符串帶有重復的"
開頭和字符串結尾,試圖刪除重復的打印相同的值。
試圖刪除雙引號它不適用於此代碼
var str = "" var item = str.includes('test'); item.replace(/\"/g, "")); console.log(item);
嘗試這個。
var someStr = 'It is "nice" to meet you';
console.log(someStr.replace(/["]+/g, ''));
首先, string.replace
不修改string
,它返回修改后的值,然后您必須將其分配給變量。
其次, string.includes
返回一個boolean
,並且沒有boolean.replace
。
嘗試這個:
let str = "\"this is test string\""; console.log(str); str = str.replace(/\"/g, ""); console.log(str);
僅當字符串以雙引號開頭和結尾時才會將其刪除,並且如果雙引號在字符串內,則將保留雙引號。
但是,很容易用'"this" will "fail"'
的字符串來欺騙它。
function process(input) { return /^".*"$/.test(input)? input.slice(1, input.length-1): input; }; [ '"This is a test"', 'This is a test', 'This is a "test"', '"this" will "fail"' ].forEach( input => console.log(process(input)));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.