Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I might asking a naive question.
But I am stuck. My requirement is do masking of data.
Following is the code snippet :
var str = substr(Test_dat,0,6);
var Test_dat1 = replace(Test_dat,str,"SampleSample");
So basically, "Test_dat" is input string and I am applying substr() function the on incoming data. And then replacing based on masking logic.
If
var Test_dat = "Vikas(vikas)";
var str = substr(Test_dat,0,5);
var Test_dat1 = replace(Test_dat,str,"SampleSample");
Output
SampleSample(vikas)
If
Input
var Test_dat = "Vikas(vikas)";
var str = substr(Test_dat,0,6);
var Test_dat1 = replace(Test_dat,str,"SampleSample");
Error Message
Function call replace is not valid : Unclosed group near index 6
I know it's because of '(' but I am not able to understand how to escape in variable "str".
Any Help!!
Change the way you use substr and replace. This code works well.
var Test_dat = "Vikas(vikas)";
var str = Test_dat.substr(0,6);
var Test_dat1 = Test_dat.replace(str,"SampleSample");
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Regex pattern for following example test test123 .first string between A-za-z and second string should be A-Za-z0-9
examples:
hello world123 (true)
123 hello123 (false)
You can use like ^[a-zA-Z]+[ ][a-zA-Z0-9]+$
function CheckValidation() {
var str = document.getElementById("txtInput").value;
var pattern = new RegExp("^[a-zA-Z]+[ ][a-zA-Z0-9]+$");
var res = pattern.test(str);
document.getElementById("para").innerHTML = res;
}
<input type="text" id="txtInput" />
<button onclick="CheckValidation()">Check</button>
<p id="para"></p>
Used the below and working fine
var pattern = new RegExp("^[a-zA-Z]+[ a-zA-Z0-9]+$");
var res = pattern.test(str);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I wrote a small function to split a string stored in variable data,
var data = "Apple|Banana";
var _res = data.split('|');
After printing _res on the console, it is Printing as Apple,Banana.
I am looking for an output where each String is printed on a newline, like,
Apple
Banana
your variable _res is an Array because it was created after splitting data. Hence it is getting printed as it is.
If you want a newline print, you need to manually do it. See below Code as an example.
Use case when you want to iterate over your input:
var data = 'Apple|Banana'; //Assuming your data variable
var _res = data.split('|');
_res.forEach(function(element) {
console.log(element);
});
Use case when you just want to test in console and alert:
var data = 'Apple|Banana'; //Assuming your data variable
var _res = data.split('|').join('\n');
alert(_res);
console.log(_res);
It sounds like you want the output as a string, in which case you shouldn't use split (which returns an array), but .replace - replace all |s with newlines:
const res = 'Apple|Banana'.replace(/\|/g, '\n');
console.log(res);
Or, with alert:
const res = 'Apple|Banana'.replace(/\|/g, '\n');
alert(res);
You should check Escape notation. You can encode special character which will have special meaning in string.
\n is used to create line breaks in string.You can split() string by , and join() by \n.
let str = 'Apple,Banana'
let newStr = str.split(',').join('\n')
console.log(newStr);
let str = 'Apple,Banana'
document.querySelector('div').innerHTML = str.split(',').join('<br>')
<div><div>
If my understanding to your problem is correct, you want to split the string using "|" and "," characters. In that case you can use pass regex value in your split method parameter.
var _res = d.data.split(/[,|]+/);
You can use this site to generate your regex https://www.regextester.com/
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I try to regex for not contain some character.
I need to show /%7(.*?);/g which dose not contain "=".
I try to input
?!xx=1
and change to
?!( (.?)=(.?) )
But it dose not work.
Please help. Thanks.
//Here is my simple regex
reg = /%7((?!xx=1).*?);/g ;
//Here is my string
str = "%7aa; %7bb=11; %7cc=123; %7xx=1; %7yy; %7zz=2;"
//I need
%7aa; and %7yy;
Instead of using a negative lookahead, try using a ^ block:
const reg = /%7([^=;]+);/g;
The ([^=;]+) bit matches any non-=, the condition you're looking for, and non-;, the character at the end of your regex.
I left the capture group in since your question's regex also contains it.
const reg = /%7([^=;]+);/g;
const str = "%7aa; %7bb=11; %7cc=123; %7xx=1; %7yy; %7zz=2;"
const matches = str.match(reg);
console.log(matches);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I used the replace() function to remove the _pc and keep the 1, but it's not working...
function testing()
{
var code = "a1_pc"; //The initial stuff
alert(code); //Printing -> a1_pc
var number = code.split("a"); //Remove the "a"
alert(number); //Printing again -> ,1_pc
number = number.slice(1); //Remove the ","
alert(number); //Printing again -> 1_pc
number = number.replace("_pc", "");
alert(number); //Returns nothing...
}
Your above solution should work perfectly and does so in the example below.
The problem must lay somewhere else within your code.
var text = '1_pc';
text = text.replace("_pc", "");
console.log(text);
if you are certain it is the replace() function causing the problems, you can use either of these 2 alternatives.
If you know that the last 3 characters are always _pc, you could use substring to find all the other characters instead.
var text = '1_pc';
text = text.substring(0, text.length - 3);
console.log(text);
Or very similiar to the solution above, you could use slice which is essentially a much cleaner version of the substring solution.
var text = '1_pc';
text = text.slice(0, -3);
console.log(text);
You can use split() javascript function and get first occurrence of string.
split("string which you want to",limit as 1 for first occurrence only)
var res = text.split("_",1);
it will return 1
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am trying to replace a string in javascript using regex but not able to do it. What I am trying is:
var str = [{"Contact":["{"name":"abc","address": "xyz","PhoneNumber": "08976"}", "{"name":"abc","address": "xyz","PhoneNumber": "08976"}","{"name":"abc","address": "xyz","PhoneNumber": "08976"}","{"name":"abc","address": "xyz","PhoneNumber": "08976"}"]}]
str.replace(/"\\{/g,"\\{") // replace every instance of "{ with just {
I want to replace all instance of "{ with just { and all instance of }" with just }. How can I do 2 replacement together and where I am going wrong in 1 replacement as the replacement that I am using is not actually happening.
Escape { with \ once. Escaping in the replacement string is not necessary.
str.replace(/"\{/g,"{")
// "[{"Contact":[{"name":"abc","address": "xyz","PhoneNumber": "08976"}", {"name":"abc","address": "xyz","PhoneNumber": "08976"}",{"name":"abc","address": "xyz","PhoneNumber": "08976"}",{"name":"abc","address": "xyz","PhoneNumber": "08976"}"]}]"
BTW, str in the question is not a string literal.
You can do both replacement in single replace call:
var repl = str.replace(/"(\{)|(\})"/g, '$1$2');
Make sure str is a valid string
You can use built-in JSON parsing to avoid use of regex
If you meant the string to be an actual string...
// Be sure that it's a string
var str = '[{"Contact":["{"name":"abc","address": "xyz","PhoneNumber": "08976"}", "{"name":"abc","address": "xyz","PhoneNumber": "08976"}","{"name":"abc","address": "xyz","PhoneNumber": "08976"}","{"name":"abc","address": "xyz","PhoneNumber": "08976"}"]}]';
// Assign the output to a variable and use single backslash to escape `{`. You're
// using the literal regex construct here / ... /
result = str.replace(/"(\{)|(\})"/g,"$1$2");
And to replace both "{ to { and }" to }, you can use capture groups, backreferences and an "or" regex operator (|).
jsfiddle