www.webdeveloper.com
Results 1 to 4 of 4

Thread: Convert Text in a Span

  1. #1
    Join Date
    Jul 2012
    Posts
    2

    Convert Text in a Span

    I need to force the word views to be Chinese characters in this without changing the number before it.
    <span class="title_date">591&nbsp;views</span>

    I was gonna use
    $j('.title_date').text('討論區');
    I know the characters here does not say views, I'm just using it as an example.

    But that would remove the number. Any ideas?

  2. #2
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,273
    Use a regular expression with something like this (whitout jQuery).

    Code:
    var str='<span class="title_date">591&nbsp;views</span>';
    newStr=str.replace(/(\d+&nbsp;)views/g,"$1看法");
    alert(newStr);
    The $1 is the first subpattern of the regular expression (one or more digits followed by &nbsp; ) and the characters 看法 could say view or views ?
    Last edited by 007Julien; 07-02-2012 at 06:43 PM.

  3. #3
    Join Date
    Jul 2012
    Posts
    2
    Quote Originally Posted by 007Julien View Post
    Use a regular expression with something like this (whitout jQuery).

    Code:
    var str='<span class="title_date">591&nbsp;views</span>';
    newStr=str.replace(/(\d+&nbsp;)views/g,"$1看法");
    alert(newStr);
    This characters 看法 could say view or views ?
    This works for each individual string, but I need to do it dynamically based on the class. I have 300 of these that all need to be changed as soon as the page loads. I don't have the time to write it out 300 times for each string as well as the number variables change. I need to be able to change it based class title_date and skip what ever number is there and only change the string views.

    Sorry I didn't explain that before.

  4. #4
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,273
    Then use jQuery to replace the strings ! With something like this (jQuery is not my cup of the)
    Code:
    $j('.title_date').text.replaceWith(this.text().replace(/(\d+&nbsp;)views/g,"$1看法");)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles