Sorry for my bad english!
How to delete some text with javascript?
I have this in textarea: [LINK=http://www.someurl.com]Link - click[/LINK].
I want that the text between "[LINK=" and "[/LINK]" deletes
(output will be "[LINK=[/LINK]").
How to do that?

You would have to use the replace string.

var myString = "this is a string\n that needs \n\n to be on a newline";
var myString2 = myString.replace(/\n/g, "<br>");

document.write (myString2);

Ok my bad on the post, i wasnt writing to you. Anyways, you have to use the substring method. Sorry for the above post.

Actually, you could do either way (from fobos posts) depending on how you approach the text. If the text value inside textarea contains known string format and length that cannot be changed, you could use substring method. If you want to search for the substring, you could use regular expression with match() function instead of replace(). A sample to use match() function is below.

// i.e.
string1 = "[LINK]blah blah yah[/LINK]"
resultString = string1.match(/\[link\]([^\[]*)\[/i)

// if the substring is found, the result of resultString would be an array size 2
// resultString[0] is the whole matching -> "[LINK]blah blah yah["
// resultString[1] is the string you want -> "blah blah yah"
// if the text is not match, resultString will be null

The sample illustrates how to match text using regular expression; however, there is an assumption -- the string must NOT contain "[" or it will match only a portion of the string.

Ok. Thanks!
(I do this with substring)!
Another question:
How to check how many times in the text some word repeats?
EG: "[BOLD]Someting what user write[/BOLD] Something what user write [BOLD]Somethind what user write[/BOLD]"
How to check how many times the "[BOLD]" repeats?

To count occurrence of a text, you could use match() and replace() while you are counting. The way to do is to match for the substring, increment the found number, and then remove the substring found. Each time you match, you do the same thing until you have gone through the whole string.

Can you write example?
(I tried, but it doesn't work)

For an easy way to do it without optimization is to search for the pattern from the starting of the string every time. In this case, it is as followed:

<script type="text/javascript">
function countMatched(str) {
  var found = 0
  var copiedStr = str.toString()

  // match a substring start with '[link' up to the first '[' char which is
  // supposed to be from '[/link]'
  // substring inside [link] and [/link] must not contain '[' char
  while (copiedStr.match(/\[link[^\[]/i)) {
    // found one, increment the count
    found += 1
    // delete the first occurrence of the match
    // note that the option 'g' is not used in this replace
    copiedStr = copiedStr.replace(/\[link[^\[]/i,"")

  return found

If you want to optimize it, you need to find the first index of '[link' found in the string and add that value to the match() argument. If you don't want to modify string at all, you need to find the last index of '[/link]' in the string for the starting index for each search.

Thank you very much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.