Replace all characters with "_" except the 1° character of each word

Asked

Viewed 228 times

2

I want to replace a sentence every character by _ except the 1° character of each word In Javascript.

Q____ S_________ U__ F____ T____ O_ C_______ P__

This would be the idea to use in large texts. I want to create a function within this code in Javascript:

function maiuscula(id){

    var letra=document.getElementById(id).value;
    letra=letra.split("");
    var tmp="";
        var Word="_";

    for(i=0;i<letra.length;i++){


    if(letra[i-1])
        {
          if(letra[i-1]==" ")
            {
                letra[i]=letra[i].replace(letra[i],letra[i].toUpperCase());
            }
        }

    else
    {
        letra[i]=letra[i].replace(letra[i],letra[i].toUpperCase());
    }

    tmp+=letra[i];

     }

    document.getElementById(id).value=tmp;

To be used in an input:

<input type="text" id="billing:firstname" onkeyup="maiuscula('billing:firstname')" name="billing[firstname]" value="" title="Nome" class="t1 required-entry"/>
  • want to replace all characters of a word, except the first one, with "_"? that’s it?

  • 1

    And in what programming language do you want to do this? Have you tried anything? Have you had any experience with programming? Your question is being denied because it is not clear.

  • I am voting to close this question as out of scope because there is no specific programming doubt in it, as per the documentation.. You can [Edit] the posting at any time if you think it is possible to leave in accordance with the guidance.

  • @You want to replace all characters except the first letter of each word (uppercase or lowercase)?

  • Obg ! I tried yes in javascript but as a beginner I did not succeed.

  • Hello stderr - so the precept that replaces the text leaving apanas to 1° capital letter preferably

Show 1 more comment

2 answers

3

Another alternative is to use the String.replace together with the regular expression /\B\w/g:

var valor = 'stack overflow em portugues';

var substituto = valor.replace(/\B\w/g, '_');
// s____ o_______ e_ p________

Where \B is the opposite of \b, \b is limited to corresponding to an exact pattern or word, depending on the position in which it is inserted, for example the expression \b\w corresponds to f and b in foo bar, the other way around \w\b corresponds to o and r.

The expression \B\w corresponds to oo and ar and \w\B to fo and ba. In this other question you have more details about: What good is a Boundary (\b) in a regular expression?

The modifier g indicates that a global search should be made, not return the first time the pattern is matched.

To display the remaining uppercase letters, use the string.toUpperCase:

var valor = 'stack overflow em portugues';
var substituto = valor.replace(/\B\w/g, '_').toUpperCase();

console.log(substituto);
// S____ O_______ E_ P________

  • Thank you guys! It worked great.

  • @Claudio If possible mark the answer as "accept" by clicking the arrow below the score!

1

The following logic will solve your problem:

var text = "QUOTE";

 function replaceString(text){
    var textRefact;
    for(var i = 0; i < text.length; i++){
       if(i == 0){
       textRefact = text.charAt(i);
    }else{
       textRefact += "_";
    }
 }
   return textRefact;
 }

var newText = replaceString(text);

console.log(newText);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.