Khurram
Khurram

Reputation: 59

How could I compare strings in jquery match

I am able to search and replace a string while finding the first word, can I match first 2 or 3 words or can I match the complete sentence including spacing

// Enlish text = "Name is not valid."
if ($(this).text().match("^Current")) {
    $(this).text('Nom est pas valable.');
}

Upvotes: 0

Views: 2908

Answers (2)

Md. Salahuddin
Md. Salahuddin

Reputation: 1072

^ is to match the beginning of the data.

var text = "Current in this world, Current in Current bar in it for example Current in Current for and again Current in world";

And if you want to check beginning of data then use

text.match(/^Current in this world/).

g is for global search. Meaning it'll match all occurrences.

if(matches = text.match(/Current in Current/g)) {
  console.log(matches);
}

Upvotes: 1

Zakaria Acharki
Zakaria Acharki

Reputation: 67505

Should work, see basic example bellow :

var example1 = "Current foo bar some extra test here";
var example2 = "foo Current bar";

if (example1.match("^Current foo bar")) 
    alert('1 ---- Nom est pas valable.');

if (example2.match("^Current")) 
    alert('2 ---- Nom est pas valable.');

Upvotes: 1

Related Questions