简体   繁体   English

如何在jquery中匹配字符后添加元素?

[英]How can I add element after match character in jquery?

Trying to place an element after match second or more dots in a text if it has a specific number of characters.如果文本具有特定数量的字符,则尝试在匹配文本中的第二个或更多点之后放置元素。 Example:例子:

<div id="mytext">
This is just a example. I need to find a solution. I will appreciate any help. Thank you. 
</div>

<script>
var chars = 55;

if ($('#mytext').text().length > chars){
//add <br> after first dot found after number of chars specified.
}
</script> 

... The output would be: ...输出将是:

This is just a example. I need to find a solution. I will appreciate any help.<br> 
Thank you.

You can try this你可以试试这个

 var chars = 55; if ($('#mytext').text().length > chars){ var text = $('#mytext').text(); // div text var chars_text = text.substring(0, chars); // chars text var rest = text.replace(chars_text, '').replace(/\\./g,'. <span>After Dot</span>'); // rest of text and replace dot of rest text with span $('#mytext').html(chars_text+rest); // apply chars and rest after replace to the div again }
 span{ color: red; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="mytext"> This is just a example. I need to find a solution. I will appreciate any help. Thank you. </div>

Note: if you just need to replace the next one dot after chars you can use '.'注意:如果您只需要替换字符后的下一个点,您可以使用'.' instead of /\\./g而不是/\\./g

this way : With JQUERY Substring这样:使用 JQUERY 子字符串

    <p>
    this is test string with jquery . test 1  2 3 4 45 5 . test test test
    </p>
    <b></b>


    <script>
    var a = $('p').text();
    var _output = '';
    var _allow_index = 40;
    if (a.length > _allow_index)
    {
      var x = a.split('.');
      for(var i=0; i<x.length; i++)
        { if (_output.length < _allow_index) { _output+=x[i]+'.'; } }
    }
    else { _output = a; }
    $('b').html(_output + '<br>'+a.substr(_output.length,a.length));
    </script>

Doing that doesn't seem to be a very good practise, for instance length may vary for localised languages.这样做似乎不是一个很好的做法,例如长度可能因本地化语言而异。 Besides, you're assuming you have a plain text, rather than an HTML text and length is different in both cases.此外,您假设您有一个纯文本,而不是 HTML 文本,并且两种情况下的长度都不同。 You may want to use html() instead of text().您可能希望使用 html() 而不是 text()。 However here is a way for the given case:但是,对于给定的情况,这是一种方法:

var container = $('#mytext');
var length = 55;
var insert = '<br/>';
var text = container.text().trim(); // text() or html()
var dotPosAfterLength = text.indexOf(".", length);
if (dotPosAfterLength != -1) {
    container.html(
        text.substring(0, dotPosAfterLength+1)
        +insert
        +text.substring(dotPosAfterLength+1)
    );
}

You just need to add this property in CSS.你只需要在 CSS 中添加这个属性。

<div id="mytext">
This is just a example. I need to find a solution. 
I will appreciate any    help. Thank you. 
</div>
<style>
div#mytext{

word-wrap: break-word;
 }
</style>

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

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