I'm trying to build an html form that generates structured JSON code from the input values. This form uses repeat fields.
I have to modify name attributes in cloned fields.
In Javascript I have a string 'menu-item[0][offers][0][price]'.
I would like to replace 'menu-item[0]' with 'menu-item[1]', as in example at http://regexr.com/3gpnt
I'm using RegExp, but is not required.
This is my experiment, but it doesn't work.
var string = 'menu-item[0][offers][price]';
var itemName = 'menu-item';
var regExp = new RegExp(itemName + '\[(.*?)\]', "");
var newString = string.replace(regExp, itemName + '[1]');
console.log(newString);
alert(newString);
Returns 'menu-item[0][offers][price]'.
Test on jsfiddle https://jsfiddle.net/lorenzodetomasi/dmnd7f9L/
Thank you.
Related
I'm trying to convert this long JS regex to C#.
The JS code below gives 29 items in an array starting from ["","常","","に","","最新","、","最高"...]
var keywords = /(\ |[a-zA-Z0-9]+\.[a-z]{2,}|[一-龠々〆ヵヶゝ]+|[ぁ-んゝ]+|[ァ-ヴー]+|[a-zA-Z0-9]+|[a-zA-Z0-9]+)/g;
var source = '常に最新、最高のモバイル。Androidを開発した同じチームから。';
var result = source.split(keywords);
But the C# code below gives a non-splitted single item in string[].
var keywords = #"/(\ |[a-zA-Z0-9]+\.[a-z]{2,}|[一-龠々〆ヵヶゝ]+|[ぁ-んゝ]+|[ァ-ヴー]+|[a-zA-Z0-9]+|[a-zA-Z0-9]+)/g";
var source = #"常に最新、最高のモバイル。Androidを開発した同じチームから。";
var result = Regex.Split(source, keywords);
Many questions in Stack Overflow are covering relatively simple expressions only, so I cannot find my mistakes.
What am I missing?
Your RegEx is wrong, you should not start and end with '/' or '/g' You specify a string in the constructor, not a JavaScript Regex (with '/ /' syntax.). That's a Javascript syntax.
Actually the same applies to JavaScript when you use a string constructor like this:
var regex = new RegExp('//'); // This will match 2 slashes
Here is a C# example code
string keywords = #"(\ |[a-zA-Z0-9]+\.[a-z]{2,}|[一-龠々〆ヵヶゝ]+|[ぁ-んゝ]+|[ァ-ヴー]+|[a-zA-Z0-9]+|[a-zA-Z0-9]+)";
string source = #"常に最新、最高のモバイル。Androidを開発した同じチームから。";
string [] res = Regex.Split(source, keywords);
string single = "";
foreach ( string str in res )
single += "'" + str + "',";
Console.WriteLine("{0}", single);
I have a problem with replace like this:
I have an array test :
test: Object
boolean: "true"
integer: "0"
values|+|0: "option_1"
values|+|1: "option_2"
and then i do parse like this:
for(var data in test){
for(var input in test[data]){
var input_aux = input.split('|+|');
if(input != ''){
$('table#'+table_id+' tbody td.'+input_aux[0]+' small').each(function(){
var text_highlighted = $(this).text().replace(new RegExp('(' + test[table][input] + ')', 'gi'), '<b>$1<\/b>');
$(this).html(text_highlighted);
}}}
what i'm trying to accomplish is to match the data from the array like option_1 that is in that table exactly option_1 and change the html of it to <b>option_1</b>.
And it's working fine my problem is like when i have the same key but different value like in the example above, it will highlight only option_2 and can't understand why, any idea?
The issue is the fact you are doing replacements even if there is no match. So you are reading the text() of all the elements and replacing it with that text. So you wipe out all of the existing html.
So check to see if there is a match before you do the replacement.
var re = new RegExp('(' + test[table][input] + ')', 'gi');
var txt = $(this).text();
if (txt.match(re)) {
var text_highlighted = txt.replace(re, '<b>$1<\/b>');
$(this).html(text_highlighted);
}
other option would be to use contains selector.
I need to do some functions on some text field contents before submitting the form, like checking the validity of the customer registeration code, having the customer name as his code in customers table appending an incrementing number.
I don't want to do it after the form is submitted becuase I need it to be displayed in the code field before submitting the form.
My code:
function getCode(){
var temp = document.getElementById("temp").value ;
var d = parseInt(document.getElementById("temp").value) + 1;
document.getElementById("customernumber").value = d;
document.getElementById("code").value = document.getElementById("name").value+"-"+ d;
}
It all works fine, but the last line of code developed the code WITH the spaces between the code.
A couple ways to remove spaces...
Using regex: string.replace(/ /g,'');
Splitting the string by spaces and combining the array with no delimiter:
string.split(' ').join('');
var str = "ab cd ef gh ";
str = str.replace(/\s+/g,"");
I have a string in which I want to replace an occurence which I am not being able to achieve following is the code
var code="user_1/has some text and user_1? also has some text";
newcode=code.replace(/user_1//g,'_');
One more thing if i have to replace a string from another string how to do?
example.
var replacestring="user_1";
var code="user_1/some value here for some user";
var newcode=code.replace(/+replacestring+/g,'_');
/ is a special char thus needs to be escaped with \ before it:
var code = "user_1/has some text and user_1? also has some text";
var newcode = code.replace(/user_1\//g, '_');
alert(newcode);
Live DEMO
if you want to replace all user_1, use this:
var code = "user_1/has some text and user_1? also has some text";
var newcode = code.replace(/user_1/g, '_');
alert(newcode);
Live DEMO
Escape the / in the regex using \
newcode=code.replace(/user_1\//g,'_');
For your comment
#Vega I have another confusion. can i use a value in a string to pass
instead of user_1/ for replacement? what would be the syntax?
You can initialize RegEx object like below,
var userName = 'user_1/';
var newcode = code.replace(new RegExp(userName, 'g'), '_');
Read More about RegEx
The pattern in this code does not replace the parenthesis. I've also tried "/(|)/g".
var re = "/[^a-z]/g",
txt = navsel.options[i].text.split(" ")[0], // here I get the text from a select and I split it.
// What I expect is strings like "(en)" , "(el)" etc
txt = txt.replace(re," ")
Thanks in advance
Your regex is a string, this will try to replace that exact string. Regex objects don't have quotes around them, just the delimiters. Try it like this:
var re = /[^a-z]/g,
txt = navsel.options[i].text.split(" ")[0], // here I get the text from a select and I split it.
txt = txt.replace(re," ");
Or if you prefer strings (and a more explicit type):
var re = new RegExp("[^a-z]", "g")