Solved

Insert html into a string

Posted on 2007-12-02
5
755 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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,…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…

691 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