• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 780
  • Last Modified:

Insert html into a string

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
simshp
Asked:
simshp
1 Solution
 
Jaime OlivaresSoftware ArchitectCommented:
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
 
ZvonkoSystems architectCommented:
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
 
ZeonFlashCommented:
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
 
ZvonkoSystems architectCommented:
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
 
simshpAuthor Commented:
Thanks, I based my solution on this answer
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now