简体   繁体   English

jQuery:对字符串进行一些操作

[英]Jquery: Doing some operations on strings

I have a string like this: 我有一个像这样的字符串:

Heading
Some interesting text here
HeadingSome interesting text hereHeading
Some interesting text here
Heading
Some interesting text here

What I want to do, is to add another heading under the third heading so it would end up looking like this: 我想做的是在第三个标题下添加另一个标题,这样最终看起来像这样:

Heading
Some interesting text here
HeadingSome interesting text hereHeadingHeading
Some interesting text here
Heading
Some interesting text here

I'm looking for a generalized method to select any heading and append stuff to it. 我正在寻找一种通用的方法来选择任何标题并向其添加内容。

Here's a second and third example: 这是第二个和第三个示例:

I have a string. 我有一个字符串。 Let's not use any DOM terms because they cause confusion. 我们不要使用任何DOM术语,因为它们会引起混淆。

My name is Mark. My name is Mark. My name is Mark. My name is Mark.

I want to add the string 'and my car is red" to the third repeat of 'My name is Mark.' 我想在“我的名字是马克”的第三次重复中添加字符串“并且我的车是红色的”。

My name is Mark. My name is Mark. My name is Mark and my car is red. My name is Mark.

Here's another example: 这是另一个例子:

My car is red. My car is red. My car is red. My car is red.

On the fourth repeat I want to change the text to 'My car is blue.' 在第四次重复中,我想将文本更改为“我的车是蓝色的”。

My car is red. My car is red. My car is red. My car is blue.

Thanks! 谢谢!

I asked a potentially related question that I wasn't able to find an answer for. 我问了一个潜在的相关问题,但我找不到答案。 But everyone there says it's easy to do with regex: Javascript String Library 但是那里的每个人都说使用正则表达式很容易: Javascript String Library

Edit: Added regex to tags. 编辑:将正则表达式添加到标签。

I am so confused. 我感到很困惑。 Apparently people have take some issue with the question. 显然,人们对此问题有些疑问。 If you think the question is missing some information, please ask, I'll do my best to create a sensible question. 如果您认为该问题缺少某些信息,请提出问题,我将尽力提出一个明智的问题。

Edit: Added more examples. 编辑:添加了更多示例。

Thanks! 谢谢!

This seems to work... 这似乎有效...

// headingIndex starts at 1...
function appendTo(currentText, headingIndex, newText) {
    var arr = currentText.split('Heading');
    arr[headingIndex] += newText;
    return arr.join('Heading');
}

It will split the entire string by the text " Heading " so you basically have a 1-based-index array of the " Heading " subsections, which then can easily be appended to then joined back together again. 它将用文本“ Heading ”分割整个字符串,因此您基本上拥有“ Heading ”小节的基于1的索引数组,然后可以轻松地将其附加并再次结合在一起。 You could change this to replace the text completely if that's what you want to do. 如果您要这样做,可以更改此选项以完全替换文本。

For selecting a specific item, you can look at the index filters (eq = equals, lt = less than, etc), such as: $('h2').eq(3).append(...) 要选择特定项目,可以查看索引过滤器(eq =等于,lt =小于等),例如:$('h2')。eq(3).append(...)

EDIT 编辑

Seeing as your request is with regards to working as strings, the above could still be applied, if you wrap it like this: 鉴于您的要求是关于字符串的使用,如果您像这样包装它,以上内容仍然适用:

var snippet = $('<div>'+stringToManipulate+'</div>');
snippet.find('h2').eq(3).html(snippet.find('h2').eq(3).html()+'more text...');
stringToManipulate = snippet.html();

Granted, that is specific code, but it is just to show off the general idea. 当然,这是特定的代码,但这只是为了炫耀总体思路。 I hope that helps. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM