sundowatch
sundowatch

Reputation: 3103

Javascript searcing and replacing?

I want to change a string which start 'a' and end 'n'.

For example: "action" I want to replace 'ctio' and all starting 'a' and finishing 'n' with ''.

How can do it?

Upvotes: 0

Views: 121

Answers (4)

Josnidhin
Josnidhin

Reputation: 12504

try this following

str.replace(/\ba(\w+)n\b/igm,'');

to the question in the comment use th following comment

var sub = "hello";
str.replace(/(<)(\w+)(")/igm,"$1" + sub + "$3");

Upvotes: 1

Robusto
Robusto

Reputation: 31883

in Javascript:

var substitute = "\"";
var text = "action";
var text = text.replace(/\b(a)([a-z]+?)(n)\b/gim,"$1" + substitute + "$3");
// result = a"n ... if what  you really want is a double quote here

Upvotes: 2

Alec
Alec

Reputation: 9078

I'm not really sure what you're trying to do, but I'm guessing going from "action" to "ctio"?

var foo = 'action';
if (foo.substr(0,1)=='a' && foo.substr(-1,1)=='n') {
  var bar = foo.substr(1,foo.length-2);
  alert(bar); // ctio
}

Upvotes: 1

kennytm
kennytm

Reputation: 523224

return theString.replace(/\ba[a-z]*n\b/ig, '')

Upvotes: 4

Related Questions