gpt4 book ai didi

jquery - 检查文本区域内的变量文本

转载 作者:行者123 更新时间:2023-12-01 08:37:01 25 4
gpt4 key购买 nike

我在页面 config.php 中有一个文本区域,在加载到另一个页面 send.php 之前,我可以在其中保存文本,并用 PHP 替换一些占位符:

我的占位符类似于:{name}、{surname}、{others}、{sometext}、...

示例页面config.php

<textarea>Dear {name}, this is the list of your products: {others}</textarea>

然后,当我加载 send.php 时:

<?php

$info_user = get_info_user($id);

$text = load_text_from_page_config();

$replacements = array(
'({name})' => $info_user['name'], //John
'({surname})' => $info_user['surname'] //Smith
);
$text = preg_replace( array_keys( $replacements ), array_values( $replacements ), $text);

echo '<textarea>' . $text . '</textarea>';

所以在我的例子中我得到:

<textarea>Dear John, this is the list of your products: {others}</textarea>

当我单击提交按钮时,我想检查我的文本区域中是否仍有一些占位符。 (在我的例子中 {others} 不存在)。在本例中,我想在 JavaScript/jQuery 中显示警报:

alert("There are still placeholder in your text. Retry again");

我的最后一个问题是:如何使用 jQuery 查找由 { 和 } 分隔的文本,但我不知道其中的确切文本?

$(document).ready(function(){

$("#submit-button").on("click", function(e) {

e.preventDefault();

var text = $("textarea").val();

if ( //here the check ) {

return false;

}

});

});

最佳答案

要实现此目的,您可以使用正则表达式来查找包含在 {} 中的任何值:

$('textarea').each(function() {
var hasTags = /\{.*\}/gi.test($(this).text());
console.log(hasTags);
});
textarea { width: 100%; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea>Lorem ipsum {dolor} sit amet consectetur adipiscing {elit}</textarea>
<textarea>Lorem ipsum dolor sit amet consectetur adipiscing elit</textarea>

请注意,这是一个贪婪匹配,因此如果文本区域中的任何点有 { 后跟 } ,它将通过该测试。

关于jquery - 检查文本区域内的变量文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52408886/

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