views:

84

answers:

2

hi, my string data as as follow.

var HmtlStr = "<span>My names is <u>KERBEROS</u>. AGE: 29, my eyes <b>BROWN</b>.</span"

result must be like this which i want;

<span>My names is <u>Kerberos</u>. Age: 29, my eyes <b>Brown</b>.</span

thank you very much for your help, already now.

+4  A: 

Use a function in a replace to change the strings:

HmtlStr = HmtlStr.replace(
  /([A-Z])([A-Z]+)/g,
  function(a,m1,m2) {
    return m1 + m2.toLowerCase();
  }
);

Edit:

The built in toLowerCase method handles most characters, you just have to include them in the set in the regular expression ([A-ZÖİŞÜĞÇ]) so that they are handled. To handle the few characters that the built in method doesn't cope with, you can make a function that replaces those first:

function localLowerCase(str) {
  str = str.replace(
    /İ/g,
    function(m){
      var i = "İ".indexOf(m);
      return i != -1 ? "ı"[i] : m;
    }
  );
  return str.toLowerCase();
}

You can easily add more characters for the function to handle by adding them in the /İ/ pattern, the "İ" string and the replacement in the "ı" string.

Guffa
Thank you very much. Your suggestion works perfectly. But i have a problem more. My language is Turkish and Turkish language characters set include special characters. That all like this; İ,ı,Ş,ş,Ü,ü,Ö,ö,Ğ,ğ,Ç,ç. When function perform with this chracters does'nt work. Do you have any suggestion for this situation?
Kerberos
@Kerberos: The built in method handles most of those characters, you just have to include them in the regular expression. For those that aren't handled as you like, you can make a function that replaces those first. I added some code above.
Guffa
Thank you again. When i use regex with [A-ZÖİŞÜĞÇ] definition , special chracters turn to lowerCase without "İ" as you suggest. Sorry, i could'nt understand. Where must i use localLowerCase function in regex snippet?
Kerberos
I mean, when ([A-Z])([A-Z]+) condition is in the event of string. I must use localLowerCase function.
Kerberos
@Kerberos: You include the characters in the regular expression so that they are handled, then you use `localLowerCase(m2)` instead of `m2.toLowerCase()` to handle the characters that aren't handled the way you like by `toLowerCase`.
Guffa
@Guffa, thank tou very much for everything. This was very big issue for me. That solved by your help. Best regards.
Kerberos
Hi again, if not problem for you i want ask a last question. When i want to exclude html tags from string. how must i use regex? Clearly i want use regex function just text data.
Kerberos
+5  A: 

Perhaps the CSS text-transform: capitalize would work?

Brian
That Would Change Every Word In The Text, So It Would Not Give The Result That Is Requested.
Guffa
@Guffa: Not if the text-transform is in the `<b>` or `<u>` tag definitions
fudgey
@fudgey: Then it still doesn't give the requested result, as "AGE" is not changed.
Guffa
@Brian There's also an internationalisation issue in all browsers with regards to the Turkish alphabet and `text-transform`, so as far as I can tell Javascript really is the way to go here.
Yi Jiang