Solved

Insert html into a string

Posted on 2007-12-02
5
729 Views
Last Modified: 2012-06-27
Hello there ..
I am not sure how to go about this regex problem. I need to insert <strong> ... </strong> tags into a string at a specific point eg:
Every string has the following pattern:  "You want to make a change! You think about y... "  - a string of words until the "!" and then further text.
I need to insert the <strong> tag  at the start of the string and then </strong> after the "!" character. Is there a way to do this in with one pass over the whole string, returning the desired result ?
Thanks
(using vb.net 2003)
0
Comment
Question by:simshp
5 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20392742
you can do it without regex, something like:

Dim s as String  = "You want to make a change! You think about y... "
s = s.Insert(IndexOf("!", s)+1, "</strong>").Insert(0, "<strong>")

but always you have to check if the ! is present, first:
If (s.Contains("!") Then s = s.Insert(IndexOf("!", s)+1, "</strong>").Insert(0, "<strong>")

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 20392759
First problem is that you have to define what characters belong to the sentance ending with the exclamation mark. My first guess would be: all lovercase, uppercase characters, blanks, coma, dash, but excluding dot, xclamation char and quota characters. So you see it is diffecult to make it 100 percent matching.

0
 
LVL 17

Expert Comment

by:ZeonFlash
ID: 20392785
Unfortunatley I'm not at an environment that I can test this with, but I think the following regex should get you close to what you want.  You can use it to find all sentences that begin with an upper case character and end with an exclamation point.  You should be able to use it in conjunction with the Regex.Replace.
.*[A-Z]+.*!

Open in new window

0
 
LVL 63

Accepted Solution

by:
Zvonko earned 250 total points
ID: 20392788
Here some example in JavaScript:
<script>
 

var myStr = 'Hello there: You want to make a change! You think about y... "  - a string of words until the "!" and then further text. Here more example!';
 

var newStr = myStr.replace(/([A-Z][a-zA-Z\s\,\-]+!)/g,"<strong>$1</strong>");

alert(newStr)

</script>

Open in new window

0
 

Author Closing Comment

by:simshp
ID: 31412230
Thanks, I based my solution on this answer
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now