gpt4 book ai didi

javascript - 去除引号以外的所有空格

转载 作者:可可西里 更新时间:2023-11-01 00:44:29 27 4
gpt4 key购买 nike

我在 preg_replace 中使用 '/\s+/' 开关来删除我传入 PHP 数组的 Javascript 代码中的所有空格:

preg_replace('/\s+/', '',
("
function(uploader)
{
if(uploader.files.length > 1)
{
uploader.files.splice(1, uploader.files.length);

apprise('You can not update more than one file at once!', {});
}
}
"))

这是一个非常基本的 Javascript 缩小。在 PHP 文件(源)中,我可以拥有完全可读的函数代码,而在浏览器中,它的页面主体最终像单行字符串:

function(uploader,files){console.log('[PluploadUploadComplete]');console.log(files);},'QueueChanged':function(uploader){if(uploader.files.length>1){uploader.files.splice(1,uploader.files.length);apprise('Youcannotupdatemorethanonefileatonce!',{});}}

如您所见(或预期),这确实会影响引号中的字符串并生成不带空格的消息(例如:Youcannotupdatemorethanonefileatonce!)。

这个问题有解决办法吗?除了嵌入在单引号中的部分外,我可以删除字符串中所有空格吗?

最佳答案

要匹配单引号内以外的空格字符,请使用:

$regex = "~'[^']*'(*SKIP)(*F)|\s+~";

您可以将其直接弹出到您的 preg_replace() 中。

例如:$replaced = preg_replace($regex,"",$input);

选项 2:多种报价

如果你可能有单引号或双引号,使用这个:

$regex = "~(['"]).*?\1(*SKIP)(*F)|\s+~";

这是如何运作的?

| 交替的左侧,我们匹配完整的 “带引号的字符串”,然后故意失败,并跳到字符串中的下一个位置。在右边,我们匹配任何空格,我们知道它是右边的空格,因为它没有被左边的表达式匹配。

引用

How to match (or replace) a pattern except in situations s1, s2, s3...

关于javascript - 去除引号以外的所有空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24075926/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com