0

I want to replace a number withing string since it can be done in regex but i don't know much about it here's my code

var rowNo=6;
var tName=jQuery(this).attr('name');

tName will be in this format

input_[3][InvoiceNo]

and I want to remove 3 and replace with rowNo value in regex what i tried was this

replace(/[0-9]+/, rowNo)

but this replaces all numbers with rowNo. I don't want to do that, what I want is to replace 3 with rowNo.

3 Answers 3

1

Replace number inside []

replace(/\[[0-9]+\]/, '['+rowNo+']')

var str = 'input_[3][InvoiceNo]',
  rowNo = 4;
alert(str.replace(/\[[0-9]+\]/, '[' + rowNo + ']'));

Sign up to request clarification or add additional context in comments.

1 Comment

hey that u misunderstood my question i want to replace all number within that bracket not 3
1

Replace number with brackets,

var rowNo = 8;
var str = 'input_[3][InvoiceNo]';

alert(str.replace(/\[\d+\]/, '[' + rowNo + ']')); // returns input_[8][InvoiceNo]

https://jsfiddle.net/8ewdtv8L/

Comments

0

You can include [] to find string to be replaced

.replace(/\[[0-9]\]+/, "[" + rowNo +"]")

var str = "input_[3][InvoiceNo_8_9]";    
var rowNo = 6;
str = str.replace(/\[[0-9]\]+/, "[" + rowNo +"]")
document.write(str);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.