How to split string from particular word in javascript? - javascript

[
{
docPath: "c:\Project\Indofebweb\Attachment\images\indofab.png",
}
]
I want to split the string from \Attachment and get the rest of the string e.g
\Attachment\images\indofab.png . How can I do this?

For this particular string you can use :
var arr=test.split("Indofebweb");
var restString=arr[1]; //-----contains string \Attachment\images\indofab.png
You can make your own logic using split() function.
UPDATED CODE----TRY THIS----BELOW HTML
<!DOCTYPE html>
<html>
<body>
<p> a string display :</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var str = "c:\\Project\\Indofebweb\\Attachment\\images\\indofab.png";;
var arr=str.split("Indofebweb");
document.getElementById("demo").innerHTML = arr[1];
}
</script>
</body>
</html>

use str.match()
path.match(/\\Attachment.*/);
this will return everything after and including "\Attachment". Keep in mind that backslashes need to be escaped.

Related

How can I put middle dot character inside .join()?

How can I put middle dot character inside .join() for split arrays?
I should put this
<span>·</span>
inside
{product.user.map(ul => ul.names).join(" ")}
If I get the questions correctly, you just need to use dot instead of empty quotation marks.
A Sample modified from w3schools.
function myFunction() {
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var x = document.getElementById("demo");
x.innerHTML = fruits.join('.');
}
<!DOCTYPE html>
<html>
<body>
<p>Click the button to join the array elements into a string.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
</body>
</html>
Yes you can, this should work:
<h1>{product.user.map(ul => ul.names).join(' <span>·</span> ')} </h1>

How to replace "#[" anywhere in the string

My question is about regular expressions. I want to replace "#[" anywhere in the string. Tried with [#[] expression, but it's not working, because it replaces # anywhere in the string.
you can use the regex
/#\[/g
function replace(str){
return str.replace(/#\[/g, '');
}
console.log(replace('#[]'));
console.log(replace('abcd#[absd]ahah#[ahah'));
<!DOCTYPE html>
<html>
<body>
<p>Click the button to replace "Microsoft" with "W3Schools" in the paragraph below:</p>
<p id="demo">Visit Microsoft! #[</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var str = document.getElementById("demo").innerHTML;
var res = str.replace(/#\[/g, "W3Schools");
document.getElementById("demo").innerHTML = res;
}
</script>
</body>
</html>

Simple Javascript on html textbox conversion. Doesn't work twice

I'm using the replace method and if I type in "test test" only the first test gets converted to good so it'll become "good test". I'm at a loss on why this is happening. On a side question, if I was to add 20 other words that I would like to replace, would I have to create 20 different str.replace?
<!DOCTYPE html>
<html>
<body>
<p>Click the button to replace "Test" with "Good"</p>
<textarea id="firstbox"></textarea>
<textarea id="secondbox"></textarea>
<button onclick="myFunction()">Change</button>
<script>
function myFunction() {
var str = document.getElementById("firstbox").value.toLowerCase()
var res = str.replace("test", "good");
document.getElementById("secondbox").value = res;
}
</script>
</body>
</html>
Any help would be greatly appreciated
Use regex, change "good" to /good/g
function myFunction() {
var str = document.getElementById("firstbox").value.toLowerCase()
var res = str.replace(/test/g, "good");
document.getElementById("secondbox").value = res;
}
<p>Click the button to replace "Test" with "Good"</p>
<textarea id="firstbox"></textarea>
<textarea id="secondbox"></textarea>
<button onclick="myFunction()">Change</button>

split hindi word maintaining conjuncts using javascript

i am trying out bellow code
<!DOCTYPE html>
<html>
<body>
<p>Click the button to display the array values after the split.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var str = "कसौटी";
var res = str.split("");
document.getElementById("demo").innerHTML = res;
}
</script>
</body>
</html>
my result should be क, सौ, टी
however it turns out to be क,स,ौ,ट,ी
what exactly can be done to get the above result
is there a way to split hindi character while maintaing the conjuncts
any way in which we split utf -8 encoded characters then merge them back to get above result
please provide details in code , on how exactly it can be done

Javascript replace words from a string

I have this text in javascript:
'This is a %{component} with a %{value}'
I should replace all words that look like %{\w+} with <span>word</span>
The final result should look like:
'This is a <span>component</span> with a <span>value</span>'
As already written use String.replace and a RegEx:
var originalString = 'This is a %{component} with a %{value}';
var replacedString = originalString.replace(/%\{(\w+)\}/g, '<span>$1</span>');
// "This is a <span>component</span> with a <span>value</span>"
Is this what you are looking for?
<!DOCTYPE html>
<html>
<body>
<p id="demo">This is a %{component} with a %{value}</p>
<button onclick="myFunction()">Test</button>
<script>
function myFunction()
{
var str=document.getElementById("demo").innerHTML;
var n=str.replace("%{","< span >").replace("}","< /span >");
document.getElementById("demo").innerHTML=n;
}
</script>
</body>
</html>
You can do that with regex in javascript / jquery.
Have a look here: http://de.selfhtml.org/javascript/objekte/regexp.htm

Categories