473,414 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

use string.replace with regex to insert <wbr> tags inside a long string

Hi,

I have a user text. Some of the strings in the text could be longer than my display so I thought of using string.replace with regex to insert <wbr> tags after a fixed number of characters. For example:

Expand|Select|Wrap|Line Numbers
  1. bla bla bla abcdefghijklmnopqrstabcd
will be:

Expand|Select|Wrap|Line Numbers
  1. bla bla bla abcd</wbr>efgh</wbr>ijkl</wbr> ....
What is the syntax for string.replace(regex, str) I should use?

Thanks
Sep 7 '07 #1
5 6319
gits
5,390 Expert Mod 4TB
hi ...

welcome to TSDN ...

its a little bit trickier then you might think ;) ... let me show you an example and i commented the code inside there ...

Expand|Select|Wrap|Line Numbers
  1. // we want linebreaks after 5 chars for the 
  2. // following text
  3. var text = 'abcd efg hij klmn opqrs tx';
  4.  
  5. // we will match 5 chars with the following regEx
  6. // AND the rest :) that we might split again
  7. var re = /(^.{0,5})(.{0,})/;
  8.  
  9. // the next will be a list of substrings
  10. var text_items = [];
  11.  
  12. // one item
  13. var text_item  = null;
  14.  
  15. // now we 'explode' the text
  16. var explode = function(t) {
  17.     var expl_txt = t.match(re);
  18.     text_item = expl_txt[1];
  19.     text = expl_txt[2];
  20. };
  21.  
  22. explode(text);
  23.  
  24. while (text_item != '') {
  25.     text_items.push(text_item);
  26.     explode(text);
  27. }
  28.  
  29. // join the listitems with desired string
  30. var new_text = text_items.join('</wbr>');
  31.  
  32. alert(new_text);
  33.  
kind regards
Sep 8 '07 #2
hi,

Your code helped me allot. I now understand better regular expressions and gorups.

I`ve found a function elsewhere that does the same and I`m investigating the differences. http://snippets.dzone.com/posts/show/869
I`m not sure which one is supposed to be faster.

Thanks
Sep 10 '07 #3
gits
5,390 Expert Mod 4TB
hi ...

try it and in case you find out something ... please post it here ... would be interesting to know ;) ...

kind regards
Sep 10 '07 #4
Hi,

I`m back with some news.

apparently my initial goal was rather easy to accomplish. By playing with your code I realized I can get it with just one line.

This code will insert </wbr> tag to break words longer then 10 characters. It won`t count spaces and line breaks because the browsers deals with them automatically.

Expand|Select|Wrap|Line Numbers
  1. var str = 'ThisStringIsTooLong and some normal words'
  2. str.replace([/S]{10},'$&</wbr> ';
  3.  
Then I faced another problem. In my application I`m replacing links with <a> tags. If the link text is longer then 10 characters I`ll break it. So I need a regex that will break the text of the link but not the address inside the <a>. For example:

Expand|Select|Wrap|Line Numbers
  1. <a href="http://www.example.com">http://www<wbr/>.example.c<wbr/>om<a/>
The next code will do it:

Expand|Select|Wrap|Line Numbers
  1. str = str.replace(/(?:<[^>]+>)|(.{10})/g,'$&<wbr/>');
  2. str = str.replace(/><wbr\/>/,'>');
  3.  
The second line will remove unwanted <wbr/> that will appear after each html tag.
It`s important to note that all the < and > characters in the normal text are escaped in a previous stage.
Thanks for you help guys.
Sep 12 '07 #5
Hi,

Thanks for your code.

I have modified it a bit to make it work in some special cases.

Posting the modified code below -

Expand|Select|Wrap|Line Numbers
  1. function wordwrap(str, len) {
  2.     len = len || 10;
  3.     /* Regex to remove spaces before the start of any html tag */ 
  4.     str = str.replace(/ +</g,'<');
  5.     /* Regex to insert <wbr/> after every html tag or a text of 'len' characters 
  6.     var re = new RegExp("(?:<[^>]+>)|([^<&]{0,"+len+"})","g");
  7.     str = str.replace(re,'$&<wbr/>');
  8.     str = str.replace(/><wbr\/>/g,'>');
  9.     return str;
  10. }
  11.  
Thanks
Sep 5 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: RBalbat | last post by:
Hello all, I have encountered an unexpected problem where if I render a table (in IE or Opera) and there are cells that contains URLs with long query strings, even though I specify the table...
2
by: johkar | last post by:
I have a rather long unbroke string (a URL) which I would like to break at certain points using XSL. I can't seem to get translate() to work: translate(.,'/',,'&lt;wbr>/&lt;/wbr>') I don't know...
3
by: Ben | last post by:
I Have a datagrid that i have placed inside a <div> section so that it becomes scrollable, but now i have a new problem. I would like the <div> to auto start at the bottom of the scroll (newest...
4
by: rjl | last post by:
Is there a way to assign an arraylist inside an arraylist as a column? i have arrayList list, which has 2 Strings and an arrayList with 3 values. I would like the following columns then in a...
7
by: bojan.pikl | last post by:
Hi, I am making a calculator. I have a double wich I am parsing to string and writing to label. How could I make that double or string would be only 14 length + space for negative sign (-). So I...
7
by: Russell Hind | last post by:
I want to create an array of key value pairs. This code compiles fine: KeyValuePair<string, object> kvps = { new KeyValuePair<string,object>( "int", 5 ), new KeyValuePair<string,object>(...
18
by: Kyro | last post by:
New to C# (migrating from Delphi) and I'm not sure how to get a delimited string into an array of string. input: string looks like - 01-85-78-15-Q11 output: an array with elements - ...
5
by: foolproofplan | last post by:
Hey everyone. I am running into a problem with unique ids that need to be compared in two xml files. The actual object name is represented with its unique id later in the xml file, so i need to do...
15
by: removeps-groups | last post by:
How to wrap text in <ptag if the text has no spaces and is very long? Here is an example: ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.