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 2 years ago.
Improve this question
I'll make this extremely simple
I have a function that comes across a string as such by design: water 'thing one'
I need to split this string by the first space, since there are spaces elsewhere.
I've tried many regex expressions like / .*? /, but they can only match two consecutive spaces.
How do I do this?
Thanks in advance.
If you just want the portion of the string before and after the first space, you could use regex replacement here:
var input = "water 'thing one'";
var first = input.replace(/[ ].*$/, "");
var second = input.replace(/^\S*[ ]/, "");
console.log("first part: " + first);
console.log("second part: " + second);
You can capture them using String#match with this regex:
/(.*?) (.*)/
const text = "water 'thing one'";
const [, first, second] = text.match(/(.*?) (.*)/);
console.log('first:', first);
console.log('second:', second);
Related
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 months ago.
Improve this question
First part:
I want to replace the spaces in a string to \n but the first space I want it to be replaced with a double like this: \n\n
var string = "this is something random “
Result needed:
var string = "this\n\nis\nsomething\nrandom\n"
Second part:
I want to replace the string quotes with backticks.
var string = "this\n\nis\nsomething\nrandom\n"
Result needed:
var string = `this\n\nis\nsomething\nrandom\n`
First Part
For what you want, you can just call a replace function to replace the first occurrence, then a replaceAll/regex to replace all others:
let input = "Hello World !"
function replaceFirstThenAll(string, lookfor, replace) {
return string
// replace the first occurance
.replace(lookfor, replace.repeat(2))
// now all others
.replace(new RegExp(lookfor, 'g') /* global regex */, replace)
}
console.log(replaceFirstThenAll(
input,
" ",
"\n"
)) // Hello\n\nWorld\n !
Second Part
The way that you declare a string doesn't matter in js, so there's no way to replace it. No matter which method you use, the string is the same.
"String1" == 'String1' // true
'String2' == `String2` // true
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 3 years ago.
Improve this question
I'm wondering how to show the first two characters and replace all last character of a string by symbol *.
Ex: 121,121,121 -> 12x,xxx,xxx .
Thanks
I love using regex when it comes to replace string according to some pattern.
var p = '121,121,121';
var regex = /(?<=.{2})([0-9])/gm;
console.log(p.replace(regex, 'x'));
You can use substring and regular expression. See the sample below.
var str = "121,121,121";
var res = str.substring(0, 2) + '' + str.substring(2, str.length).replace(/[0-9]/g,"x");
alert(res);
Just use substring and replace with a simple regex (to single out digits and keep commas and other punctuation):
const str = "121,121,121";
const obfuscated = `${str.substring(0, 2)}${str.substring(2).replace(/\d/g, "*")}`;
console.log(obfuscated);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have this string:
"This thing (123, 12) (2005.03 - 2011.12)"
I want to convert it to:
"This thing (2005.03 - 2011.12)"
Meaning remove text between two first parenthesis: (123, 12). But only if there are two or more parenthesis following in same string. So a string like
"Another thing (2005.05 - 2011.08)"
should be left as it is.
How can I do it with javascript?
You can use String.replace() with regex like this https://regex101.com/r/X7ioxu/1
var regex = /(\(.+?\))\s?\(/g;
var str1 = "This thing (123, 12) (2005.03 - 2011.12)";
var str1 = "This thing (2005.03 - 2011.12)";
alert(str1.replace(regex,'('));
alert(str2.replace(regex,'('));
With the data given, this works
var str = "This thing (123, 12) (2005.03 - 2011.12)";
var parts = str.split(/(?=\()/g); // split on ( with lookahead
if (parts.length==3) parts.splice(1,1)
str = parts.join("")
console.log(str)
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 9 years ago.
Improve this question
I would like to all alphanumeric data +
only these following 4 special character are allowed.
' (single quote)
- (hyphen)
. (dot)
single space
I tried this :
var userinput = $(this).val();
var pattern = [A-Za-z0-9_~\-!##\$%\^&\*\(\)]+$
if(!pattern.test(userinput))
{
alert('not a valid');
}
but it is not working.
First, you need to enclose the string in / to have it interpreted as a regex:
var pattern = /[A-Za-z0-9_~\-!##\$%\^&\*\(\)]+$/;
Then, you have to remove some unallowed characters (that regex is matching more than you specified):
var pattern = /^[A-Za-z0-9 '.-]+$/;
The second one is what you need. Complete code:
var userinput = $(this).val();
var pattern = /^[A-Za-z0-9 '.-]+$/;
if(!pattern.test(userinput))
{
alert('not a valid');
}
Besides, check what this points to.
"Not working" is not a helpful description of your problem.
I'd suggest this regular expresion:
^[a-zA-Z0-9\'\-\. ]+$
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 8 years ago.
Improve this question
How can I add a character after the second characters in string?
Ex: I want this : 1700 to become this: 17:00.
The simplest way, in my opinion, is using substr to split the string and concatenate the other character in between. This will work no matter the length of the second part:
var str = "1700";
str.substr(0,2)+":"+str.substr(2);
this would do the magic:
"1700".replace(/(..)$/, ":$1")
alternative you can do something like look from behind in substr too like:
var string = "1700";
string = string.substr(0, string.length -2) + ":" + string.substr(-2, 2);
they both also work on something like: 900 wich will turn into 9:00
the regex and the substr line both do the same. if you want readability i'd consider the regex.
You could do this:
'1700'.match(/../g).join(':')
The following regexp accepts 3+ chars:
'700'.match(/^(.+)(..)$/).slice(1, 3).join(':') // "7:00"
Shortest and probably fastest solution:
s[0]+s[1]+":"+s[2]+s[3]
Without using regexp, you can use substr --
string = string.substr(0,2) + ":" + string.substr(2);
You can use substring. Consider the following:
string1 = "1700";
string2 = ":";
string1 = string1.substring(0,2) + string2 + string1.substring(2);
alert(string1);