我正在寻找一个足够聪明的 javascript 函数来删除一大段文本(实际上是一个段落)的最后一句话。显示复杂性的一些示例文本:
<p>Blabla, some more text here. Sometimes <span>basic</span> html code is used but that should not make the "selection" of the sentence any harder! I looked up the window and I saw a plane flying over. I asked the first thing that came to mind: "What is it doing up there?" She did not know, "I think we should move past the fence!", she quickly said. He later described it as: "Something insane."</p>
现在我可以拆分 . 并删除数组的最后一个条目,但这不适用于以 ? 或 ! 结尾的句子有些句子以引号结尾,例如 something: "stuff."
function removeLastSentence(text) {
sWithoutLastSentence = ...; // ??
return sWithoutLastSentence;
}
如何做到这一点?什么是合适的算法?
编辑 - 长文本是指段落中的所有内容,句子是指实际句子(不是一行),所以在我的示例中,最后一句话是:He后来将其描述为:“有些疯狂。” 当那一个被移除时,下一个是 她不知道,“我认为我们应该越过栅栏!”,她很快说。/
最佳答案
定义您的规则: //1. 一个句子以大写字母开头 //2. 一个句子之前没有任何内容或 [.!?],但不是 [,:;] //3. 如果格式不正确,句子前面可以加引号,例如 ["'] //4. 如果引号后面的词是名字,在这种情况下句子可能是错误的
任何额外的规则?
定义您的目的: //1. 去掉最后一句
假设: 如果您从文本字符串中的最后一个字符开始并向后工作,那么您会将句子的开头标识为: 1.字符前的文本字符串是[.?!] OR 2. 字符前的文字串是["'],前面是一个大写字母 3. 每个 [.] 前面都有一个空格 4. 我们没有更正 html 标签 5. 这些假设并不稳健,需要定期调整
可能的解决方案: 读入您的字符串并将其拆分为空格字符,为我们提供要反向查看的字符串 block 。
var characterGroups = $('#this-paragraph').html().split(' ').reverse();
如果你的字符串是:
Blabla,这里有更多文字。有时会使用基本的 html 代码,但这不应该使句子的“选择”变得更加困难!我抬头看 window ,看到一架飞机飞过。我问第一个想到的事情:“它在上面做什么?”她不知道,“我认为我们应该越过围栏!”,她很快说道。他后来将其描述为:“有些疯狂。”
var originalString = 'Blabla, some more text here. Sometimes <span>basic</span> html code is used but that should not make the "selection" of the sentence any harder! I looked up the window and I saw a plane flying over. I asked the first thing that came to mind: "What is it doing up there?" She did not know, "I think we should move past the fence!", she quickly said. He later described it as: "Something insane."';
那么你在 characterGroups 中的数组将是:
["insane."", ""Something", "as:", "it", "described", "later", "He",
"said.", "quickly", "she", "fence!",", "the", "past", "move", "should", "we",
"think", ""I", "know,", "not", "did", "She", "there?"", "up", "doing", "it",
"is", ""What", "mind:", "to", "came", "that", "thing", "first", "the", "asked",
"I", "over.", "flying", "plane", "a", "saw", "I", "and", "window", "the", "up",
"looked", "I", "harder!", "any", "sentence", "the", "of", ""selection"", "the",
"make", "not", "should", "that", "but", "used", "is", "code", "html", "basic",
"Sometimes", "here.", "text", "more", "some", "Blabla,"]
注意: 将使用 jQuery 中的 .text() 方法删除 '' 标签和其他标签
每个 block 后面都有一个空格,所以当我们确定了句子的起始位置(通过数组索引)时,我们就会知道该空格的索引是什么,我们可以在空格占据该索引的位置拆分原始字符串从句末开始。
给我们自己一个变量来标记我们是否找到它,以及一个变量来保存我们确定为保存最后一句话开头的数组元素的索引位置:
var found = false;
var index = null;
遍历数组并查找任何以 [.!?] 结尾或以 "结尾的元素,其中前一个元素以大写字母开头。
var position = 1,//skip the first one since we know that's the end anyway
elements = characterGroups.length,
element = null,
prevHadUpper = false,
last = null;
while(!found && position < elements) {
element = characterGroups[position].split('');
if(element.length > 0) {
last = element[element.length-1];
// test last character rule
if(
last=='.' // ends in '.'
|| last=='!' // ends in '!'
|| last=='?' // ends in '?'
|| (last=='"' && prevHadUpper) // ends in '"' and previous started [A-Z]
) {
found = true;
index = position-1;
lookFor = last+' '+characterGroups[position-1];
} else {
if(element[0] == element[0].toUpperCase()) {
prevHadUpper = true;
} else {
prevHadUpper = false;
}
}
} else {
prevHadUpper = false;
}
position++;
}
如果您运行上面的脚本,它会正确地将“他”识别为最后一句话的开头。
console.log(characterGroups[index]); // He at index=6
现在您可以遍历之前的字符串:
var trimPosition = originalString.lastIndexOf(lookFor)+1;
var updatedString = originalString.substr(0,trimPosition);
console.log(updatedString);
// Blabla, some more text here. Sometimes <span>basic</span> html code is used but that should not make the "selection" of the sentence any harder! I looked up the window and I saw a plane flying over. I asked the first thing that came to mind: "What is it doing up there?" She did not know, "I think we should move past the fence!", she quickly said.
再次运行得到: Blabla,这里还有一些文字。有时会使用基本的 html 代码,但这不应该使句子的“选择”变得更加困难!我抬头看 window ,看到一架飞机飞过。我首先想到的是:“它在上面做什么?”
再次运行得到: Blabla,这里还有一些文字。有时会使用基本的 html 代码,但这不应该使句子的“选择”变得更加困难!我抬头看着 window ,看到一架飞机飞过。
再次运行得到: Blabla,这里还有一些文字。有时会使用基本的 html 代码,但这不应该使句子的“选择”变得更加困难!
再次运行得到: Blabla,这里还有一些文字。
再次运行得到: Blabla,这里还有一些文字。
那么,我认为这符合您要查找的内容?
作为函数:
function trimSentence(string){
var found = false;
var index = null;
var characterGroups = string.split(' ').reverse();
var position = 1,//skip the first one since we know that's the end anyway
elements = characterGroups.length,
element = null,
prevHadUpper = false,
last = null,
lookFor = '';
while(!found && position < elements) {
element = characterGroups[position].split('');
if(element.length > 0) {
last = element[element.length-1];
// test last character rule
if(
last=='.' || // ends in '.'
last=='!' || // ends in '!'
last=='?' || // ends in '?'
(last=='"' && prevHadUpper) // ends in '"' and previous started [A-Z]
) {
found = true;
index = position-1;
lookFor = last+' '+characterGroups[position-1];
} else {
if(element[0] == element[0].toUpperCase()) {
prevHadUpper = true;
} else {
prevHadUpper = false;
}
}
} else {
prevHadUpper = false;
}
position++;
}
var trimPosition = string.lastIndexOf(lookFor)+1;
return string.substr(0,trimPosition);
}
为它制作一个插件是微不足道的,但要注意假设! :)
这有帮助吗?
谢谢, AE
关于Javascript (jQuery) 删除长文本的最后一句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7531664/
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html
我发现ActiveRecord::Base.transaction在复杂方法中非常有效。我想知道是否可以在如下事务中从AWSS3上传/删除文件:S3Object.transactiondo#writeintofiles#raiseanexceptionend引发异常后,每个操作都应在S3上回滚。S3Object这可能吗?? 最佳答案 虽然S3API具有批量删除功能,但它不支持事务,因为每个删除操作都可以独立于其他操作成功/失败。该API不提供任何批量上传功能(通过PUT或POST),因此每个上传操作都是通过一个独立的API调用完成的
rails中是否有任何规定允许站点的所有AJAXPOST请求在没有authenticity_token的情况下通过?我有一个调用Controller方法的JqueryPOSTajax调用,但我没有在其中放置任何真实性代码,但调用成功。我的ApplicationController确实有'request_forgery_protection'并且我已经改变了config.action_controller.consider_all_requests_local在我的environments/development.rb中为false我还搜索了我的代码以确保我没有重载ajaxSend来发送
在Ruby中是否有Gem或安全删除文件的方法?我想避免系统上可能不存在的外部程序。“安全删除”指的是覆盖文件内容。 最佳答案 如果您使用的是*nix,一个很好的方法是使用exec/open3/open4调用shred:`shred-fxuz#{filename}`http://www.gnu.org/s/coreutils/manual/html_node/shred-invocation.html检查这个类似的帖子:Writingafileshredderinpythonorruby?
我正在尝试找到一种方法来规范化字符串以将其作为文件名传递。到目前为止我有这个:my_string.mb_chars.normalize(:kd).gsub(/[^\x00-\x7F]/n,'').downcase.gsub(/[^a-z]/,'_')但第一个问题:-字符。我猜这个方法还有更多问题。我不控制名称,名称字符串可以有重音符、空格和特殊字符。我想删除所有这些,用相应的字母('é'=>'e')替换重音符号,并将其余的替换为'_'字符。名字是这样的:“Prélèvements-常规”“健康证”...我希望它们像一个没有空格/特殊字符的文件名:“prelevements_routin
Rails相对较新。我正在尝试调用一个API,它应该向我返回一个唯一的URL。我的应用程序中捆绑了HTTParty。我已经创建了一个UniqueNumberController,并且我已经阅读了几个HTTParty指南,直到我想要什么,但也许我只是有点迷路,真的不知道该怎么做。基本上,我需要做的就是调用API,获取它返回的URL,然后将该URL插入到用户的数据库中。谁能给我指出正确的方向或与我分享一些代码? 最佳答案 假设API为JSON格式并返回如下数据:{"url":"http://example.com/unique-url"
我是HanamiWorld的新人。我已经写了这段代码:moduleWeb::Views::HomeclassIndexincludeWeb::ViewincludeHanami::Helpers::HtmlHelperdeftitlehtml.headerdoh1'Testsearchengine',id:'title'hrdiv(id:'test')dolink_to('Home',"/",class:'mnu_orizontal')link_to('About',"/",class:'mnu_orizontal')endendendendend我在模板上调用了title方法。htm
我去了这个website查看Rails5.0.0和Rails5.1.1之间的区别为什么5.1.1不再包含:config/initializers/session_store.rb?谢谢 最佳答案 这是删除它的提交:Setupdefaultsessionstoreinternally,nolongerthroughanapplicationinitializer总而言之,新应用没有该初始化器,session存储默认设置为cookie存储。即与在该初始值设定项的生成版本中指定的值相同。 关于
我有一个电子邮件表格。但是我正在制作一个测试电子邮件表单,用户可以在其中添加一个唯一的电子邮件,并让电子邮件测试将其发送到该特定电子邮件。为了简单起见,我决定让测试电子邮件通过ajax执行,并将整个内容粘贴到另一个电子邮件表单中。我不知道如何将变量从我的HAML发送到我的Controllernew.html.haml-form_tagadmin_email_blast_pathdoSubject%br=text_field_tag'subject',:class=>"mass_email_subject"%brBody%br=text_area_tag'message','',:nam