String Jquery or JS

Asked

Viewed 208 times

0

Personal I am in need of help on the following logic, I have a String question that always follows a pattern: What is xxx xxx or what means xxx xxx, I would like to always catch the xxx xxx.

Example:

var question1 = "What is nuclear physics"; var Pergunta2 = " What means nuclear physics"

in case I’d like to always pick up what this after what it is or what it means.

Thank you,

  • You always want to catch the first three words ?

3 answers

2


Use the split javascript itself

oque = "o que é física nuclear";
significa = "o que significa física atômica";

cortaOque = oque.split("é")[1];
console.log(cortaOque); //saída: física nuclear

cortaSignifica = significa.split("significa")[1]; 
console.log(cortaSignifica); //saída: física atômica

1

You can use the substring function as described below in the code. It removes the previous 7 characters.To learn more about this function see the link w3schools.

    
       function myFunction() {
        var str = "O que é física nuclear";
        var res = str.substring(7);
        document.getElementById("demo").innerHTML = res;
    }
      
   
    <!DOCTYPE html>
    <html>
    <body>
    
    <p>Click the button to extract characters from the string.</p>
    
    <button onclick="myFunction()">Try it</button>
    
    <p id="demo"></p>
    
     </body>
    </html>

  • The answer from the friend below is a little complex as it will create an array.

0

You can do it like this: defines two variables, var o_q_eh = 'O que é:' and var o_q_significa = 'O que Significa ';. waiting q vc use the html tag label to create the question text... <label></label> would look like this: <label id="pergunta1">O que é física nuclear</label>

use jquery to get the text of id="pergunta1" using: var pergunta1 = $('#pergunta1').text(); then use substring to remove the q needs, or concatenate the variables: var o_q_eh;and var o_q_significa with the rest of the text of each <label> that you need. if I understand what you need...

Browser other questions tagged

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